MATLAB Data Type
MATLAB Data Type
Category Description
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
1-23
1 Functions By Category
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
Numeric
1-24
Programming and Data Types
String to Numeric
Numeric to String
Other Conversions
1-25
1 Functions By Category
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
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
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
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
1-29
1 Functions By Category
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”
1-30
Programming and Data Types
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
Object-Oriented Programming
1-32
Programming and Data Types
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