List Of Syntax For MATLAB
Have you been searching for syntax on MATLAB? We bring it for you.
Programming
is much easier when you have a list of syntax in the next window. So,
with that in mind, bookmark this one and keep this open whenever you're
working on MATLAB. Here it is...
VARIOUS
|
foo ... end where foo
in { if, for, while, ... }
|
block (grouping statements, especially when statements are
not expressions)
|
|
...
|
breaking lines (useful when end-of-line and/or indentation
has a special meaning)
|
|
%( ... %)
|
commenting (nestable)
|
|
%
|
commenting (until end of line)
|
|
< > <= >=
|
comparison
|
|
min / max
|
comparison (min / max (binary or more))
|
function MYFUNCTION %MYFUNCTION the very first comment line is displayed in the help table of contents % % the remaining lines are displayed when getting help for MYFUNCTION % |
documentation comment
|
|
== ~= eq ne isequal
isequalwithequalnans
|
equality / inequality (deep)
|
|
( ... )
|
grouping expressions
|
|
eval
|
runtime evaluation
|
|
case-sensitive
|
tokens (case-sensitivity (keywords, variable
identifiers...))
|
|
[a-zA-Z][_a-zA-Z0-9]*
|
tokens (variable identifier regexp)
|
|
lowercasenoseparator
|
tokens (what is the standard way for scrunching together
multiple words)
|
|
=
|
variable assignment or declaration (assignment)
|
|
global v1 v2
|
variable assignment or declaration (declaration)
|
FUNCTIONS
|
inline('...x...y...')(1)
|
anonymous function
|
|
f(a,b,...)
|
function call
|
|
f a b ...
|
function call
|
|
f
|
function call (with no parameter)
|
function retval = f(para1, para2) retval = ... |
function definition
|
function f(para1, para2) ... |
function definition (procedures)
|
function f(varargin) for i=1:nargin ...(varargin{i})
end |
function definition (variable number of arguments)
|
|
return(2)
|
function return value (breaks the control flow)
|
|
no syntax needed(3)
|
function return value (function body is the result)
|
|
=
val;
|
function return value (setting the result)
|
|
evalin('caller', ...)
|
runtime inspecting the caller information
|
CONTROL FLOW
|
continue / break
|
breaking control flow (continue / break)
|
|
return(2)
|
breaking control flow (returning a value)
|
try a
catch ...
end |
exception (catching)
|
|
error
|
exception (throwing)
|
|
if c, ..., end
|
if_then
|
if c ...
end |
if_then
|
|
if c1, b1, elseif c2,
b2, else, b3, end
|
if_then_else
|
if c b1
elseif c2 b2
else b3
end |
if_then_else
|
|
for i = 10:-1:1, ...,
end
|
loop (for each value in a numeric range, 1 decrement)
|
|
for i = 1:10, ..., end
|
loop (for each value in a numeric range, 1 increment (see
also the entries about ranges))
|
|
for i = 1:3:10, ...,
end
|
loop (for each value in a numeric range, free increment)
|
|
while c, ..., end
|
loop (while condition do something)
|
switch val case v1
...
case {v2,v3}
...
otherwise
...
end |
multiple selection (switch)
|
|
,
|
sequence
|
|
;
|
sequence
|
|
end-of-line
|
sequence
|
TYPES
|
t(e)
|
cast (computed conversion (calls an internal or a
user-defined function))
|
|
typecast(e,t)
|
cast (downcast (need runtime checking))
|
|
typecast(e,t)
|
cast (upcast)
|
|
mutability is the
default
|
mutability, constness (type of a mutable value)
|
REFLEXIVITY AND OBJECT ORIENTED PROGRAMMING
|
class
|
class declaration
|
|
first parameter(4)
|
current instance
|
|
class
|
get the type/class corresponding to an
object/instance/value
|
|
ismethod
|
has the method
|
|
method(object, para)
|
method invocation
|
|
method(object)
|
method invocation (with no parameter)
|
|
methods
|
methods available
|
|
o2 = o(5)
|
object cloning
|
|
class_name(...)
|
object creation
|
|
isa
|
testing class membership
|
PACKAGE AND MODULE
|
package declare(6)
|
declare
|
|
all files in package
directory are exported. files in /private sub-directory are not exported, but
can be used by the package itself
|
declare (selective export)
|
|
addpath
|
import (everything into current namespace)
|
|
/
|
package scope
|
STRINGS
|
s(n)
|
accessing n-th character
|
|
char
|
ascii to character
|
|
'z'
|
character "z"
|
|
(done automatically
when applying mathematical operations on char, such as +)
|
character to ascii
|
|
repmat
|
duplicate n times
|
|
s(n:m)
|
extract a substring
|
|
strfind
|
locate a substring
|
|
t=strfind(s,p), t(end)
|
locate a substring (starting at the end)
|
|
nothing - just remove
";" at the end of the expression, and it will print it
|
simple print (on any objects)
|
|
disp
|
simple print (on any objects)
|
|
printf
|
simple print (printf-like)
|
|
sprintf
|
sprintf-like
|
|
[string1 string2]
|
string concatenation
|
|
strcmp
|
string equality & inequality
|
|
== ~=
|
string equality & inequality
|
|
length
|
string size
|
|
'...'
|
strings (with no interpolation of variables)
|
|
upper / lower(7)
|
upper / lower case character
|
|
upper / lower
|
uppercase / lowercase / capitalized string
|
BOOLEANS
|
false
|
false value
|
|
array containing at
least one false value
|
false value
|
|
0(8)
|
false value
|
|
0.0
|
false value
|
|
''
|
false value
|
|
[]
|
false value
|
|
{}
|
false value
|
|
~
|
logical not
|
|
| / &
|
logical or / and (non short circuit (always evaluates both
arguments))
|
|
|| / &&
|
logical or / and (short circuit)
|
|
anything not false
|
true value
|
|
logical
|
type name
|
LISTS AND BAGS
|
a(1,:), a(2,:)
|
2 lists from a list of couples
|
|
[e l]
|
adding an element at the beginning (list cons) (return the
new list (no side-effect))
|
|
[l e]
|
adding an element at the end (return the new list (no
side-effect))
|
|
a(2:end)
|
all but the first element
|
|
find(a == 3)
|
find an element
|
|
ismember
|
is an element in the list
|
|
any(9)
|
is the predicate true for an element
|
|
strcat
|
join a list of strings in a string using a glue string
|
|
a(a == 3)
|
keep elements (matching)
|
|
a(end)
|
last element
|
|
,
|
list concatenation
|
|
[ a, b, c ](10)
|
list constructor
|
|
[a b]
|
list of couples from 2 lists
|
|
[a.(:)]
|
list out of a bag
|
|
size
|
list size
|
|
length
|
list size
|
|
numel
|
list size
|
|
a(i)
|
list/array indexing
|
|
a.(e)
|
lookup an element in a association list
|
|
unique(11)
|
remove duplicates
|
|
fliplr flipud...
|
reverse
|
|
min / max
|
smallest / biggest element
|
|
sort(12)
|
sort
|
|
magical: sin(x)
computes sin on each element
|
transform a list (or bag) in another one
|
|
magical: a binary
function or operator is appliied on each element
|
transform two lists in parallel
|
|
cell
|
type name
|
OTHER DATA TYPES
|
{}
|
computable tuple (these are a kind of immutable lists
playing a special role in parameter passing) (empty tuple)
|
|
t{:}
|
computable tuple (these are a kind of immutable lists
playing a special role in parameter passing) (using a tuple for a function
call)
|
|
h.k
|
dictionary (access: read/write)
|
|
struct(a, b, c, d)
|
dictionary (constructor)
|
|
isfield
|
dictionary (has the key ?)
|
|
fieldnames
|
dictionary (list of keys)
|
|
struct2cell
|
dictionary (list of values)
|
|
rmfield
|
dictionary (remove by key)
|
|
struct
|
dictionary (type name)
|
|
a:b
|
range (inclusive .. inclusive)
|
|
.
|
record (selector)
|
|
{ a, b, c }
|
tuple constructor
|
MATHEMATICS
|
+ / - / * / /
|
addition / subtraction / multiplication / division
|
|
bitand / bitor / bitxor
|
bitwise operators (and / or / xor)
|
|
bitcmp
|
bitwise operators (bitwise inversion)
|
|
bitshift
|
bitwise operators (left shift / right shift / unsigned
right shift)
|
|
^
|
exponentiation (power)
|
|
log10
|
logarithm (base 10)
|
|
log2
|
logarithm (base 2)
|
|
log
|
logarithm (base e)
|
|
rem
|
modulo (modulo of -3 / 2 is -1)
|
|
mod
|
modulo (modulo of -3 / 2 is 1)
|
|
-
|
negation
|
|
1000, 1000., 1000.0
|
numbers syntax (integers)
|
|
mathematical
|
operator priorities and associativities (addition vs
multiplication)
|
|
mathematical
|
operator priorities and associativities (exponentiation vs
negation (is -3^2 equal to 9 or -9))
|
|
rand
|
random (random number)
|
|
rand('state',...)
|
random (seed the pseudo random generator)
|
|
sqrt realsqrt / exp /
abs
|
square root / e-exponential / absolute value
|
|
sin / cos / tan
|
trigonometry (basic)
|
|
asin / acos / atan(13)
|
trigonometry (inverse)
|
|
trunc / round / floor /
ceil
|
truncate / round / floor / ceil
|
|
single, double
|
type name (floating point)
|
|
int8, uint8, int16,
uint16, ...64
|
type name (integers)
|
SOURCE: Regaux.org

EmoticonEmoticon