This document provides an overview of syntax basics in MATLAB including:
1) How to continue statements across multiple lines using ellipses.
2) Two syntaxes for passing name-value arguments in function calls: name=value and comma-separated.
3) How to ignore outputs from functions using the tilde operator.
3. Continue Long Statements on Multiple
Lines
This example shows how to continue a statement to the next line using ellipsis (...).
s = 1 - 1/2 + 1/3 - 1/4 + 1/5 ...
- 1/6 + 1/7 - 1/8 + 1/9;
Build a long character vector by concatenating shorter vectors together:
mytext = ['Accelerating the pace of ' ...
'engineering and science'];
4. Name=Value in Function Calls
MATLAB supports two syntaxes for passing name-value arguments.
plot(x,y,LineWidth=2) name=value syntax
plot(x,y,"LineWidth",2) comma-separated syntax
Use the name=value syntax to help identify name-value arguments for functions and to clearly
distinguish names from values in lists of name-value arguments.
5. Ignore Function Outputs
Request all three possible outputs from the fileparts function.
helpFile = which('help');
[helpPath,name,ext] = fileparts(helpFile);
To ignore function outputs in any position in the argument list, use the tilde operator. For example,
ignore the first output using a tilde.
[~,name,ext] = fileparts(helpFile);
You can ignore any number of function outputs using the tilde operator. Separate consecutive tildes
with a comma. For example, this code ignores the first two output arguments.
[~,~,ext] = fileparts(helpFile);
7. Case and Space Sensitivity
MATLAB code is sensitive to casing, and insensitive to blank spaces except when defining arrays.
For example, MATLAB interprets the following statements the same way.
y = sin (3 * pi) / 2
y=sin(3*pi)/2
For example, the statement [1 sin (pi) 3] produces a much different result than [1 sin(pi) 3] does.
Error using sin
Not enough input arguments.
8. Choose Command Syntax or Function
Syntax
MATLAB has two ways of calling functions, called function syntax and command syntax.
In MATLAB, these statements are equivalent:
load durer.mat % Command syntax
load('durer.mat') % Function syntax
This equivalence is sometimes referred to as command-function duality.
All functions support this standard function syntax:
[output1, ..., outputM] = functionName(input1, ..., inputN)
9. CAVEAT !
With command syntax, MATLAB passes all inputs as character vectors (that is, as if they were
enclosed in single quotation marks) and does not assign outputs to user defined variables.
A = 123;
disp(A)
This code returns the expected result,
123
You cannot use command syntax to pass the value of A