MATLAB Basics

Basics

  • MATLAB is designed around double precision, complex numbers, and arrays
  • End any value-producing statement with ; to suppress its result display, especially in scripts
  • The comment marker is %
    • %{...%} for multi-line comments
  • MATLAB is insensitive to indentation; the end of a block is marked by end

Numeric Constants

ConstantDescription
pi
epsFloating-point relative precision
i, jImaginary unit
inf, InfInfinity, e.g.
nan, NaNIndeterminate (not a number), e.g.
intmaxLargest representable integer
intminSmallest representable integer
realmaxLargest representable positive real
realminSmallest representable positive real

Keywords

KeywordUsage
break
case
catch
classdef
continue
else
elseif
end
for
function
global
if
otherwise
parfor
persistent
return
spmd
switch
try
while

Variables

Like Python, MATLAB does not require variable declarations or type annotations.

Variable Naming Rules

  1. Must consist only of letters, digits, and underscores, and must begin with a letter
  2. Case-sensitive
  3. Length must not exceed 31 characters
  4. Must not be a keyword
  5. May reuse a constant or function name
    • After such reuse, the name no longer refers to the original constant or function

Validating Variable Names

Predefined Variables

VariableDescription
ansDefault variable that receives a result when no assignment is given
narginNumber of input arguments actually passed to a function
nargoutNumber of output arguments actually requested