An asterisk in a filename specification is used as a wildcard specifier, as described below.
Wildcards are generally used in file operations that act on
multiple files or folders. They usually appear in the file or folder
specification. MATLAB® matches all characters in the name exactly
except for the wildcard character *
, which can
match any one or more characters.
To locate all files with names that start with 'january_'
and
have a mat
file extension, use
dir('january_*.mat')
You can also use wildcards with the who
and whos
functions. To get information on
all variables with names starting with 'image'
and
ending with 'Offset'
, use
whos image*Offset
The @
sign signifies either a function handle
constructor or a folder that supports a MATLAB class.
The @
operator forms a handle to either the
named function that follows the @
sign, or to the
anonymous function that follows the @
sign.
Function Handles in General. Function handles are commonly used in passing functions as arguments
to other functions. Construct a function handle by preceding the function
name with an @
sign:
fhandle = @myfun
For more information, see Create Function Handle.
Handles to Anonymous Functions. Anonymous functions give you a quick means of creating simple functions without having to create your function in a file each time. You can construct an anonymous function and a handle to that function using the syntax
fhandle = @(arglist) body
where body
defines the body of the function
and arglist
is the list of arguments you can pass
to the function.
See Anonymous Functions for more information.
An @
sign can indicate the name of a class
folder, such as
\@myclass\get.m
See the documentation on Class and Path Folders for more information.
The colon operator generates a sequence of numbers that you can use in creating or indexing into arrays. SeeGenerating a Numeric Sequence for more information on using the colon operator.
Generate a sequential series of regularly spaced numbers from first
to last
using
the syntax first:last
. For an incremental sequence
from 6 to 17, use
N = 6:17
Generate a sequential series of numbers, each number separated
by a step
value, using the syntax first:step:last
.
For a sequence from 2 through 38, stepping by 4 between each entry,
use
N = 2:4:38
Index into multiple rows or columns of a matrix using the colon operator to specify a range of indices:
B = A(7, 1:5); % Read columns 1-5 of row 7. B = A(4:2:8, 1:5); % Read columns 1-5 of rows 4, 6, and 8. B = A(:, 1:5); % Read columns 1-5 of all rows.
Convert a matrix or array to a column vector using the colon operator as a single index:
A = rand(3,4); B = A(:);
Using the colon operator on the left side of an assignment statement, you can assign new values to array elements without changing the shape of the array:
A = rand(3,4); A(:) = 1:12;
A comma is used to separate the following types of elements.
When constructing an array, use a comma to separate elements that belong in the same row:
A = [5.92, 8.13, 3.53]
When indexing into an array, use a comma to separate the indices into each dimension:
X = A(2, 7, 4)
When calling a function, use a comma to separate output and input arguments:
function [data, text] = xlsread(file, sheet, range, mode)
To enter more than one MATLAB command or statement on the same line, separate each command or statement with a comma:
for k = 1:10, sum(A(k)), end
Use curly braces to construct or get the contents of cell arrays.
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}
For more information, see Cell Arrays
The single dot operator has the following different uses in MATLAB.
MATLAB uses a period to separate the integral and fractional parts of a number.
Add fields to a MATLAB structure by following the structure name with a dot and then a field name:
funds(5,2).bondtype = 'Corporate';
For more information, see Structures
Specify the properties of an instance of a MATLAB class using the object name followed by a dot, and then the property name:
val = asset.current_value
Two dots in sequence refer to the parent of the current folder.
Specify the folder immediately above your current folder using
two dots. For example, to go up two levels in the folder tree and
down into the test
folder, use
cd ..\..\test
A series of three consecutive periods (...
)
is the line continuation operator in MATLAB. This is often referred
to as an ellipsis, but it should be noted that
the line continuation operator is a three-character operator and is
different from the single-character ellipsis represented by the Unicode
character U+2026.
Continue any MATLAB command or expression by placing an ellipsis at the end of the line to be continued:
sprintf('The current value of %s is %d', ... vname, value)
Entering Long Character Vectors. You cannot use an ellipsis within single quotes to continue a character vector to the next line:
quote = 'This is not allowed and will generate an ... error in MATLAB.'
To enter text that extends beyond a single line, piece together
shorter character vectors using either the concatenation operator
([]
) or the sprintf
function.
Here are two examples:
quote1 = [ 'Tiger, tiger, burning bright in the forests of the night,' ... 'what immortal hand or eye could frame thy fearful symmetry?']; quote2 = sprintf('%s%s%s', ... 'In Xanadu did Kubla Khan a stately pleasure-dome decree,', ... 'where Alph, the sacred river, ran ', ... 'through caverns measureless to man down to a sunless sea.');
Defining Arrays. MATLAB interprets the ellipsis as a space character. For
statements that define arrays or cell arrays within []
or {}
operators,
a space character separates array elements. For example,
not_valid = [1 2 zeros... (1,3)]
is equivalent to
not_valid = [1 2 zeros (1,3)]
which returns an error. Place the ellipsis so that the interpreted statement is valid, such as
valid = [1 2 ... zeros(1,3)]
Use dot-parentheses to specify the name of a dynamic structure field.
Sometimes it is useful to reference structures with field names that can vary. For example, the referenced field might be passed as an argument to a function. Dynamic field names specify a variable name for a structure field.
The variable fundtype
shown here is a dynamic
field name:
type = funds(5,2).(fundtype);
See Generate Field Names from Variables for more information.
The exclamation point precedes operating system commands that you want to execute from within MATLAB.
The exclamation point initiates a shell escape function. Such a function is to be performed directly by the operating system:
!rmdir oldtests
For more information, see Shell Escape Functions.
Parentheses are used mostly for indexing into elements of an
array or for specifying arguments passed to a called function. Parenthesis
also control the order of operations, and can group a vector visually
(such as x = (1:10)
) without calling a concatenation
function.
When parentheses appear to the right of a variable name, they are indices into the array stored in that variable:
A(2, 7, 4)
When parentheses follow a function name in a function declaration or call, the enclosed list contains input arguments used by the function:
function sendmail(to, subject, message, attachments)
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. Two percent signs, %%
, serve
as a cell delimiter described in Run Code Sections.
Some functions also interpret the percent sign as a conversion specifier.
Precede any one-line comments in your code with a percent sign. MATLAB does
not execute anything that follows a percent sign (that is, unless
the sign is quoted, '%'
):
% The purpose of this routine is to compute % the value of ...
See Add Comments to Programs for more information.
Some functions, like sscanf
and sprintf
, precede conversion specifiers
with the percent sign:
sprintf('%s = %d', name, value)
The %{
and %}
symbols
enclose a block of comments that extend beyond one line.
Enclose any multiline comments with percent followed by an opening or closing brace.
%{ The purpose of this routine is to compute the value of ... %}
Note
With the exception of whitespace characters, the |
The +
sign appears most frequently as an
arithmetic operator, but is also used to designate the names of package
folders. For more information, see Packages Create Namespaces.
The semicolon can be used to construct arrays, suppress output from a MATLAB command, or to separate commands entered on the same line.
When used within square brackets to create a new array or concatenate existing arrays, the semicolon creates a new row in the array:
A = [5, 8; 3, 4] A = 5 8 3 4
When placed at the end of a command, the semicolon tells MATLAB not to display any output from that command. In this example, MATLAB does not display the resulting 100-by-100 matrix:
A = ones(100, 100);
Like the comma operator, you can enter more than one MATLAB command on a line by separating each command with a semicolon. MATLAB suppresses output for those commands terminated with a semicolon, and displays the output for commands terminated with a comma.
In this example, assignments to variables A
and C
are
terminated with a semicolon, and thus do not display. Because the
assignment to B
is comma-terminated, the output
of this one command is displayed:
A = 12.5; B = 42.7, C = 1.25; B = 42.7000
Single quotes are the constructor symbol for MATLAB character arrays.
MATLAB constructs a character array from all characters enclosed in single quotes. If only one character is in quotes, then MATLAB constructs a 1-by-1 array:
S = 'Hello World'
For more information, see Characters and Strings
The space character serves a purpose similar to the comma in that it can be used to separate row elements in an array constructor, or the values returned by a function.
You have the option of using either commas or spaces to delimit the row elements of an array when constructing the array. To create a 1-by-3 array, use
A = [5.92 8.13 3.53] A = 5.9200 8.1300 3.5300
When indexing into an array, you must always use commas to reference each dimension of the array.
Spaces are allowed when specifying a list of values to be returned by a function. You can use spaces to separate return values in both function declarations and function calls:
function [data text] = xlsread(file, sheet, range, mode)
The slash (/) and backslash (\) characters separate the elements of a path or folder name. 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'])
Square brackets are used in array construction and concatenation, and also in declaring and capturing values returned by a function.
To construct a matrix or array, enclose all elements of the array in square brackets:
A = [5.7, 9.8, 7.3; 9.2, 4.5, 6.4]
To combine two or more arrays into a new array through concatenation, enclose all array elements in square brackets:
A = [B, eye(6), diag([0:2:10])]
When declaring or calling a function that returns more than one output, enclose each return value that you need in square brackets:
[data, text] = xlsread(file, sheet, range, mode)
The tilde character is used in comparing arrays for unequal values, finding the logical NOT of an array, and as a placeholder for an input or output argument you want to omit from a function call.
To test for inequality values of elements in arrays a
and b
for
inequality, use a~=b
:
a = primes(29); b = [2 4 6 7 11 13 20 22 23 29]; not_prime = b(a~=b) not_prime = 4 6 20 22
To find those elements of an array that are zero, use:
a = [35 42 0 18 0 0 0 16 34 0]; ~a 1×10 logical array 0 0 1 0 1 1 1 0 0 1
To have the fileparts
function
return its third output value and skip the first two, replace arguments
one and two with a tilde character:
[~, ~, filenameExt] = fileparts(fileSpec);
See Ignore Function Inputs in the MATLAB Programming documentation for more information.