1 Functions By Category
Programming and Data Types
Functions to store and operate on data at either the MATLAB command line or
in programs and scripts. Functions to write, manage, and execute MATLAB
programs.
Category Description
“Data Types” Numeric, character, structures, cell arrays,
and data type conversion
“Arrays” Basic array operations and manipulation
“Operators and Operations” Special characters and arithmetic,
bit-wise, relational, logical, set, date and
time operations
“Programming in MATLAB” M-files, function/expression evaluation,
program control, function handles, object
oriented programming, error handling
Data Types
• “Numeric”
• “Characters and Strings”
• “Structures”
• “Cell Arrays”
• “Data Type Conversion”
Numeric
[ ] Array constructor
cat Concatenate arrays
class Return object’s class name (e.g., numeric)
find Find indices and values of nonzero array elements
ipermute Inverse permute dimensions of multidimensional array
isa Detect object of given class (e.g., numeric)
isequal Determine if arrays are numerically equal
isnumeric Determine if item is numeric array
isreal Determine if all array elements are real numbers
1-22
Programming and Data Types
permute Rearrange dimensions of multidimensional array
reshape Reshape array
squeeze Remove singleton dimensions from array
zeros Create array of all zeros
Characters and Strings
Description of Strings in MATLAB
strings Describes MATLAB string handling
Creating and Manipulating Strings
blanks Create string of blanks
char Create character array (string)
cellstr Create cell array of strings from character array
datestr Convert to date string format
deblank Strip trailing blanks from the end of string
lower Convert string to lower case
sprintf Write formatted data to string
sscanf Read string under format control
strcat String concatenation
strjust Justify character array
strread Read formatted data from string
strrep String search and replace
strvcat Vertical concatenation of strings
upper Convert string to upper case
Comparing and Searching Strings
class Return object’s class name (e.g., char)
findstr Find string within another, longer string
isa Detect object of given class (e.g., char)
iscellstr Determine if item is cell array of strings
ischar Determine if item is character array
isletter Detect array elements that are letters of the alphabet
isspace Detect elements that are ASCII white spaces
strcmp Compare strings
strcmpi Compare strings, ignoring case
strfind Find one string within another
strmatch Find possible matches for string
strncmp Compare first n characters of strings
strncmpi Compare first n characters of strings, ignoring case
strtok First token in string
1-23
1 Functions By Category
Evaluating String Expressions
eval Execute string containing MATLAB expression
evalc Evaluate MATLAB expression with capture
evalin Execute string containing MATLAB expression in workspace
Structures
cell2struct Cell array to structure array conversion
class Return object’s class name (e.g., struct)
deal Deal inputs to outputs
fieldnames Field names of structure
getfield Get field of structure array
isa Detect object of given class (e.g., struct)
isequal Determine if arrays are numerically equal
isfield Determine if item is structure array field
isstruct Determine if item is structure array
rmfield Remove structure fields
setfield Set field of structure array
struct Create structure array
struct2cell Structure to cell array conversion
Cell Arrays
{ } Construct cell array
cell Construct cell array
cellfun Apply function to each element in cell array
cellstr Create cell array of strings from character array
cell2struct Cell array to structure array conversion
celldisp Display cell array contents
cellplot Graphically display structure of cell arrays
class Return object’s class name (e.g., cell)
deal Deal inputs to outputs
isa Detect object of given class (e.g., cell)
iscell Determine if item is cell array
iscellstr Determine if item is cell array of strings
isequal Determine if arrays are numerically equal
num2cell Convert numeric array into cell array
struct2cell Structure to cell array conversion
Data Type Conversion
Numeric
double Convert to double-precision
1-24
Programming and Data Types
int8 Convert to signed 8-bit integer
int16 Convert to signed 16-bit integer
int32 Convert to signed 32-bit integer
single Convert to single-precision
uint8 Convert to unsigned 8-bit integer
uint16 Convert to unsigned 16-bit integer
uint32 Convert to unsigned 32-bit integer
String to Numeric
base2dec Convert base N number string to decimal number
bin2dec Convert binary number string to decimal number
hex2dec Convert hexadecimal number string to decimal number
hex2num Convert hexadecimal number string to double number
str2double Convert string to double-precision number
str2num Convert string to number
Numeric to String
char Convert to character array (string)
dec2base Convert decimal to base N number in string
dec2bin Convert decimal to binary number in string
dec2hex Convert decimal to hexadecimal number in string
int2str Convert integer to string
mat2str Convert a matrix to string
num2str Convert number to string
Other Conversions
cell2struct Convert cell array to structure array
datestr Convert serial date number to string
func2str Convert function handle to function name string
logical Convert numeric to logical array
num2cell Convert a numeric array to cell array
str2func Convert function name string to function handle
struct2cell Convert structure to cell array
Determine Data Type
is* Detect state
isa Detect object of given MATLAB class or Java class
iscell Determine if item is cell array
iscellstr Determine if item is cell array of strings
ischar Determine if item is character array
isfield Determine if item is character array
1-25
1 Functions By Category
isjava Determine if item is Java object
islogical Determine if item is logical array
isnumeric Determine if item is numeric array
isobject Determine if item is MATLAB OOPs object
isstruct Determine if item is MATLAB structure array
Arrays
• “Array Operations”
• “Basic Array Information”
• “Array Manipulation”
• “Elementary Arrays”
Array Operations
[ ] Array constructor
, Array row element separator
; Array column element separator
: Specify range of array elements
end Indicate last index of array
+ Addition or unary plus
- Subtraction or unary minus
.* Array multiplication
./ Array right division
.\ Array left division
.^ Array power
.' Array (nonconjugated) transpose
Basic Array Information
disp Display text or array
display Overloaded method to display text or array
isempty Determine if array is empty
isequal Determine if arrays are numerically equal
isnumeric Determine if item is numeric array
islogical Determine if item is logical array
length Length of vector
ndims Number of array dimensions
numel Number of elements in matrix or cell array
size Array dimensions
1-26
Programming and Data Types
Array Manipulation
: Specify range of array elements
blkdiag Construct block diagonal matrix from input arguments
cat Concatenate arrays
find Find indices and values of nonzero elements
fliplr Flip matrices left-right
flipud Flip matrices up-down
flipdim Flip array along specified dimension
horzcat Horizontal concatenation
ind2sub Subscripts from linear index
ipermute Inverse permute dimensions of multidimensional array
permute Rearrange dimensions of multidimensional array
repmat Replicate and tile array
reshape Reshape array
rot90 Rotate matrix 90 degrees
shiftdim Shift dimensions
sort Sort elements in ascending order
sortrows Sort rows in ascending order
squeeze Remove singleton dimensions
sub2ind Single index from subscripts
vertcat Horizontal concatenation
Elementary Arrays
: Regularly spaced vector
blkdiag Construct block diagonal matrix from input arguments
eye Identity matrix
linspace Generate linearly spaced vectors
logspace Generate logarithmically spaced vectors
meshgrid Generate X and Y matrices for three-dimensional plots
ndgrid Generate arrays for multidimensional functions and interpolation
ones Create array of all ones
rand Uniformly distributed random numbers and arrays
randn Normally distributed random numbers and arrays
zeros Create array of all zeros
Operators and Operations
• “Special Characters”
• “Arithmetic Operations”
• “Bit-wise Operations”
• “Relational Operations”
1-27
1 Functions By Category
• “Logical Operations”
• “Set Operations”
• “Date and Time Operations”
Special Characters
: Specify range of array elements
( ) Pass function arguments, or prioritize operations
[ ] Construct array
{ } Construct cell array
. Decimal point, or structure field separator
... Continue statement to next line
, Array row element separator
; Array column element separator
% Insert comment line into code
! Command to operating system
= Assignment
Arithmetic Operations
+ Plus
- Minus
. Decimal point
= Assignment
* Matrix multiplication
/ Matrix right division
\ Matrix left division
^ Matrix power
' Matrix transpose
.* Array multiplication (element-wise)
./ Array right division (element-wise)
.\ Array left division (element-wise)
.^ Array power (element-wise)
.' Array transpose
Bit-wise Operations
bitand Bit-wise AND
bitcmp Bit-wise complement
bitor Bit-wise OR
bitmax Maximum floating-point integer
bitset Set bit at specified position
bitshift Bit-wise shift
bitget Get bit at specified position
1-28
Programming and Data Types
bitxor Bit-wise XOR
Relational Operations
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to
Logical Operations
& Logical AND
| Logical OR
~ Logical NOT
all Test to determine if all elements are nonzero
any Test for any nonzero elements
find Find indices and values of nonzero elements
is* Detect state
isa Detect object of given class
iskeyword Determine if string is MATLAB keyword
isvarname Determine if string is valid variable name
logical Convert numeric values to logical
xor Logical EXCLUSIVE OR
Set Operations
intersect Set intersection of two vectors
ismember Detect members of set
setdiff Return set difference of two vectors
setxor Set exclusive or of two vectors
union Set union of two vectors
unique Unique elements of vector
Date and Time Operations
calendar Calendar for specified month
clock Current time as date vector
cputime Elapsed CPU time
date Current date string
datenum Serial date number
datestr Convert serial date number to string
datevec Date components
eomday End of month
1-29
1 Functions By Category
etime Elapsed time
now Current date and time
tic, toc Stopwatch timer
weekday Day of the week
Programming in MATLAB
• “M-File Functions and Scripts”
• “Evaluation of Expressions and Functions”
• “Variables and Functions in Memory”
• “Control Flow”
• “Function Handles”
• “Object-Oriented Programming”
• “Error Handling”
• “MEX Programming”
M-File Functions and Scripts
( ) Pass function arguments
% Insert comment line into code
... Continue statement to next line
depfun List dependent functions of M-file or P-file
depdir List dependent directories of M-file or P-file
function Function M-files
input Request user input
inputname Input argument name
mfilename Name of currently running M-file
nargin Number of function input arguments
nargout Number of function output arguments
nargchk Check number of input arguments
nargoutchk Validate number of output arguments
pcode Create preparsed pseudocode file (P-file)
script Describes script M-file
varargin Accept variable number of arguments
varargout Return variable number of arguments
Evaluation of Expressions and Functions
builtin Execute builtin function from overloaded method
cellfun Apply function to each element in cell array
eval Interpret strings containing MATLAB expressions
1-30
Programming and Data Types
evalc Evaluate MATLAB expression with capture
evalin Evaluate expression in workspace
feval Evaluate function
iskeyword Determine if item is MATLAB keyword
isvarname Determine if item is valid variable name
pause Halt execution temporarily
run Run script that is not on current path
script Describes script M-file
symvar Determine symbolic variables in expression
tic, toc Stopwatch timer
Variables and Functions in Memory
assignin Assign value to workspace variable
global Define global variables
inmem Return names of functions in memory
isglobal Determine if item is global variable
mislocked True if M-file cannot be cleared
mlock Prevent clearing M-file from memory
munlock Allow clearing M-file from memory
pack Consolidate workspace memory
persistent Define persistent variable
rehash Refresh function and file system caches
Control Flow
break Terminate execution of for loop or while loop
case Case switch
catch Begin catch block
continue Pass control to next iteration of for or while loop
else Conditionally execute statements
elseif Conditionally execute statements
end Terminate conditional statements, or indicate last index
error Display error messages
for Repeat statements specific number of times
if Conditionally execute statements
otherwise Default part of switch statement
return Return to invoking function
switch Switch among several cases based on expression
try Begin try block
while Repeat statements indefinite number of times
Function Handles
class Return object’s class name (e.g. function_handle)
1-31
1 Functions By Category
feval Evaluate function
function_handle
Describes function handle data type
functions Return information about function handle
func2str Constructs function name string from function handle
isa Detect object of given class (e.g. function_handle)
isequal Determine if function handles are equal
str2func Constructs function handle from function name string
Object-Oriented Programming
MATLAB Classes and Objects
class Create object or return class of object
fieldnames List public fields belonging to object,
inferiorto Establish inferior class relationship
isa Detect object of given class
isobject Determine if item is MATLAB OOPs object
loadobj User-defined extension of load function for user objects
methods Display method names
methodsview Displays information on all methods implemented by class
saveobj User-defined extension of save function for user objects
subsasgn Overloaded method for A(I)=B, A{I}=B, and A.field=B
subsindex Overloaded method for X(A)
subsref Overloaded method for A(I), A{I} and A.field
substruct Create structure argument for subsasgn or subsref
superiorto Establish superior class relationship
Java Classes and Objects
cell Convert Java array object to cell array
class Return class name of Java object
clear Clear Java packages import list
depfun List Java classes used by M-file
exist Detect if item is Java class
fieldnames List public fields belonging to object,
import Add package or class to current Java import list
inmem List names of Java classes loaded into memory
isa Detect object of given class
isjava Determine whether object is Java object
javaArray Constructs Java array
javaMethod Invokes Java method
javaObject Constructs Java object
methods Display methods belonging to class
1-32
Programming and Data Types
methodsview Display information on all methods implemented by class
which Display package and class name for method
Error Handling
catch Begin catch block of try/catch statement
error Display error message
ferror Query MATLAB about errors in file input or output
lasterr Return last error message generated by MATLAB
lastwarn Return last warning message issued by MATLAB
try Begin try block of try/catch statement
warning Display warning message
MEX Programming
dbmex Enable MEX-file debugging
inmem Return names of currently loaded MEX-files
mex Compile MEX-function from C or Fortran source code
mexext Return MEX-filename extension
1-33