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
| Constant | Description |
|---|
pi | π |
eps | Floating-point relative precision 2.2204e-16 |
i, j | Imaginary unit −1 |
inf, Inf | Infinity, e.g. 01 |
nan, NaN | Indeterminate (not a number), e.g. 00 |
intmax | Largest representable integer 2147483647 |
intmin | Smallest representable integer −2147483648 |
realmax | Largest representable positive real 1.7977e+308 |
realmin | Smallest representable positive real 2.2251e-308 |
Keywords
| Keyword | Usage |
|---|
| 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
- Must consist only of letters, digits, and underscores, and must begin with a letter
- Case-sensitive
- Length must not exceed 31 characters
- Must not be a keyword
- 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
| Variable | Description |
|---|
ans | Default variable that receives a result when no assignment is given |
nargin | Number of input arguments actually passed to a function |
nargout | Number of output arguments actually requested |