This page contains a comprehensive listing of all MATLAB® operators, symbols, and special characters.
Symbol | Role | More Information |
---|---|---|
+ |
Addition | plus |
+ |
Unary plus | uplus |
- |
Subtraction | minus |
- |
Unary minus | uminus |
.* |
Element-wise multiplication | times |
* |
Matrix multiplication | mtimes |
./ |
Element-wise right division | rdivide |
/ |
Matrix right division | mrdivide |
.\ |
Element-wise left division | ldivide |
\ |
Matrix left division (also known as backslash) | mldivide |
.^ |
Element-wise power | power |
^ |
Matrix power | mpower |
.' |
Transpose | transpose |
' |
Complex conjugate transpose | ctranspose |
Symbol | Role | More Information |
---|---|---|
& |
Logical AND | and |
| |
Logical OR | or |
&& |
Logical AND (with short-circuiting) | Logical Operators:
Short-Circuit && || |
|| |
Logical OR (with short-circuiting) | |
~ |
Logical NOT | not |
Symbol | Symbol Name | Role | Description | Examples |
---|---|---|---|---|
@ |
At symbol |
Function handle construction and reference |
The |
Create a function handle to a named function: fhandle = @myfun Create a function handle to an anonymous function: fhandle = @(x,y) x.^2 + y.^2; |
. |
Period or dot |
|
The period character separates the integral and fractional
parts of a number, such as |
Decimal point: 102.5543 Element-wise operations: A.*B A.^2 Structure field access: myStruct.f1 Object property specifier: myObj.PropertyName |
... |
Dot dot dot or ellipsis |
Line continuation |
Three or more periods at the end of a line continues the current command on the next line. If three or more periods occur before the end of a line, then MATLAB ignores the rest of the line and continues to the next line. This effectively makes a comment out of anything on the current line that follows the three periods. NoteMATLAB interprets the ellipsis as a space character. Therefore, multi-line commands must be valid as a single line with the ellipsis replaced by a space character. |
Continue a function call on the next line: sprintf(['The current value '... 'of %s is %d'],vname,value) Break a character vector up on multiple lines and concatenate the lines together: S = ['If three or more periods occur before the '... 'end of a line, then the rest of that line is ' ... 'ignored and MATLAB continues to the next line'] To comment out one line in a multiline command, use
y = 1 +... 2 +... % 3 +... 4; However, this code runs properly since the third line does not produce a gap in the command: y = 1 +... 2 +... ... 3 +... 4; |
, |
Comma |
Separator |
Use commas to separate row elements in an array, array subscripts, function input and output arguments, and commands entered on the same line. |
Separate row elements to create an array: A = [12,13; 14,15] Separate subscripts: A(1,2) Separate input and output arguments in function calls: [Y,I] = max(A,[],2) Separate multiple commands on the same line (showing output): figure, plot(sin(-pi:0.1:pi)), grid on |
: |
Colon |
|
Use the colon operator to create regularly spaced vectors,
index into arrays, and define the bounds of a
|
Create a vector: x = 1:10 Create a vector that increments by 3: x = 1:3:19 Reshape a matrix into a column vector: A(:) Assign new elements without changing the shape of an array: A = rand(3,4); A(:) = 1:12; Index a range of elements in a particular dimension: A(2:5,3) Index all elements in a particular dimension: A(:,3)
x = 1; for k = 1:25 x = x + x^2; end |
; |
Semicolon |
|
Use semicolons to separate rows in an array creation command, or to suppress the output display of a line of code. |
Separate rows to create an array: A = [12,13; 14,15] Suppress code output: Y = max(A); Separate multiple commands on a single line (suppressing output): A = 12.5; B = 42.7, C = 1.25; B = 42.7000 |
( ) |
Parentheses |
|
Use parentheses to specify precedence of operations, enclose function input arguments, and index into an array. |
Precedence of operations: (A.*(B./C)) - D Function argument enclosure: plot(X,Y,'r*') C = union(A,B) Indexing: A(3,:) A(1,2) A(1:5,1) |
[ ] |
Square brackets |
|
Square brackets enable array construction and concatenation, creation of empty matrices, deletion of array elements, and capturing values returned by a function. |
Construct a three-element vector: X = [10 12 -3] Add a new bottom row to a matrix: A = rand(3); A = [A; 10 20 30] Create an empty matrix: A = [] Delete a matrix column: A(:,1) = [] Capture three output arguments from a function: [C,iA,iB] = union(A,B) |
{ } |
Curly brackets |
Cell array assignment and contents |
Use curly braces to construct a cell array, or to access the contents of a particular cell in a cell array. |
To construct a cell array, enclose all elements of the array in curly braces: C = {[2.6 4.7 3.9], rand(8)*6, 'C. Coolidge'} Index to a specific cell array element by enclosing all indices in curly braces: A = C{4,7,2} |
% |
Percent |
|
The percent sign is most commonly used to indicate nonexecutable text within the body of a program. This text is normally used to include comments in your code. Some functions also interpret the percent sign as a conversion specifier. Two percent signs, |
Add a comment to a block of code: % The purpose of this loop is to compute % the value of ... Use conversion specifier with
sprintf('%s = %d', name, value) |
%{ %} |
Percent curly bracket |
Block comments |
The NoteWith the exception of whitespace characters, the
|
Enclose any multiline comments with percent followed by an opening or closing brace: %{ The purpose of this routine is to compute the value of ... %} |
! |
Exclamation point |
Operating system command | The exclamation point precedes operating system commands that you want to execute from within MATLAB. Not available in MATLAB Online™. |
The exclamation point initiates a shell escape function. Such a function is to be performed directly by the operating system: !rmdir oldtests |
? |
Question mark |
Metaclass for MATLAB class |
The question mark retrieves the |
Retrieve the meta.class object for class
?inputParser |
'' |
Single quotes |
Character array constructor |
Use single quotes to create character vectors that have class
|
Create a character vector: chr = 'Hello, world' |
"" |
Double quotes |
String constructor |
Use double quotes to create string scalars that have class
|
Create a string scalar: S = "Hello, world" |
N/A |
Space character |
Separator |
Use the space character to separate row elements in an array constructor, or the values returned by a function. In these contexts, the space character and comma are equivalent. |
Separate row elements to create an array: % These statements are equivalent A = [12 13; 14 15] A = [12,13; 14,15] Separate output arguments in function calls: % These statements are equivalent [Y I] = max(A) [Y,I] = max(A) |
~ |
Tilde |
|
Use the tilde symbol to represent logical NOT or to suppress specific input or output arguments. |
Calculate the logical NOT of a matrix: A = eye(3); ~A Determine where the elements of A = [1 -1; 0 1] B = [1 -2; 3 2] A~=B Return only the third output value of
[~,~,iB] = union(A,B) |
= |
Equal sign |
Assignment |
Use the equal sign to assign values to a variable. The syntax
NoteThe |
Create a matrix A = [1 0; -1 0]; B = A; B(1) = 200; |
Some special characters can only be used in the text of a character vector or string. You can use these special characters to insert new lines or carriage returns, specify folder paths, and more.
Use the special characters in this table to specify a folder path using a character vector or string.
Symbol | Symbol Name | Role | Description | Examples |
---|---|---|---|---|
| Slash and Backslash |
File or folder path separation | In addition to their use as mathematical operators, the slash and backslash characters separate the elements of a path or folder. On Microsoft® Windows® based systems, both slash and backslash have the same effect. On The Open Group UNIX® based systems, you must use slash only. | On a Windows system, you can use either backslash or slash: dir([matlabroot '\toolbox\matlab\elmat\shiftdim.m']) dir([matlabroot '/toolbox/matlab/elmat/shiftdim.m']) On a UNIX system, use only the forward slash: dir([matlabroot '/toolbox/matlab/elmat/shiftdim.m']) |
.. |
Dot dot |
Parent folder |
Two dots in succession refers to the parent of the current folder. Use this character to specify folder paths relative to the current folder. |
To go up two levels in the folder tree and down into the
cd ..\..\test |
* |
Asterisk |
Wildcard character |
In addition to being the symbol for matrix multiplication, the
asterisk Wildcards are generally used in file operations that act on
multiple files or folders. MATLAB matches all characters in the name exactly except
for the wildcard character |
|
@ |
At symbol |
Class folder indicator |
An |
Refer to a class folder: \@myClass\get.m |
+ |
Plus |
Package directory indicator |
A |
Package folders always begin with the +mypack +mypack/pkfcn.m % a package function +mypack/@myClass % class folder in a package |
There are certain special characters that you cannot enter as ordinary text.
Instead, you must use unique character sequences to represent them. Use the symbols
in this table to format strings and character vectors on their own or in conjunction
with formatting functions like compose
, sprintf
, and error
. For more information, see Formatting Text.
Symbol | Effect on Text |
---|---|
'' |
Single quotation mark |
%% |
Single percent sign |
\\ |
Single backslash |
\a |
Alarm |
\b |
Backspace |
\f |
Form feed |
\n |
New line |
\r |
Carriage return |
\t |
Horizontal tab |
\v |
Vertical tab |
\xN |
Hexadecimal number, |
\N |
Octal number, |