| MATLAB® | ![]() |
| On this page… |
|---|
This section does not include symbols used in arithmetic, relational, and logical operations. For a description of these symbols, see the top of the list. "Functions — Alphabetical List" in the MATLAB Help browser.
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 directories. They usually appear in the string containing the file or directory 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 directory 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
You can read more about function handles in Function Handles.
Handles to Anonymous Functions. Anonymous functions give you a quick means of creating simple functions without having to create M-files 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.
A MATLAB class directory contains source files that define the methods and properties of a class. All MATLAB class directory names must begin with an @ sign:
\@myclass\get.m
See Definìng Your Own Classes for more information.
The colon operator generates a sequence of numbers that you can use in creating or indexing into arrays. See Generating 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}
See Cell Arrays for more information.
The single dot operator has the following different uses in MATLAB.
Add fields to a MATLAB structure by following the structure name with a dot and then a field name:
funds(5,2).bondtype = 'Corporate';
See Structures for more information.
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
See Definìng Your Own Classes for more information.
Two dots in sequence refer to the parent of the current directory.
Specify the directory immediately above your current directory using two dots. For example, to go up two levels in the directory tree and down into the testdir directory, use
cd ..\..\testdir
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 in ASCII by the hexadecimal number 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 Strings. You cannot use an ellipsis within single quotes to continue a string to the next line:
string = 'This is not allowed and will generate an ... error in MATLAB.'
To enter a string that extends beyond a single line, piece together shorter strings 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.');
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 Using Dynamic Field Names 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
See Shell Escape Functions for more information.
Parentheses are used mostly for indexing into elements of an array or for specifying arguments passed to a called 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. Some functions also interpret the percent sign as a conversion specifier.
See Help Text for more information.
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 ...
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 %{ and %} operators must appear alone on the lines that immediately precede and follow the block of help text. Do not include any other text on these lines. |
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'
See Characters and Strings for more information.
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 directory string. 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)
![]() | Regular Expressions | Internal MATLAB Functions | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |