MATLAB Commands

Basic Command Syntax

A simple MATLAB command computes the result of the expression to the right of the equals sign and assigns the value of the result to the output variable at the left. Examples of simple MATLAB commands are

x = 5.71;
A = [1 2 3; 4 5 6; 7 8 9];
I = besseli(nu, Z);

Commands that do not terminate with a semicolon display the result at your terminal as well as assigning it to the output variable:

A = [1 2 3; 4 5 6; 7 8 9]

A =
     1     2     3
     4     5     6
     7     8     9

If you do not explicitly assign the output of a command to a variable, MATLAB assigns the result to the reserved word ans:

[1 2 3; 4 5 6; 7 8 9]

ans =
     1     2     3
     4     5     6
     7     8     9

The value of ans changes with every command that returns an output value that is not assigned to a variable. We recommend that you do not use ans in place of variables as this practice tends to lead to code that is difficult to maintain and also to programming errors. The following use of ans, for example, is not recommended:

rand(3,5);
A = ans > 0.5;

Entering More Than One Command on a Line

You can enter more than one command on the same line, provided that you terminate each command with a comma or semicolon. Commands terminated with a comma display their results when they are executed; commands terminated with a semicolon do not:

Assigning to Multiple Outputs

rand('state', 0);   
A = rand(3,5),   B = ones(3,5) * 4.7;   C = A./B

A =
    0.9501    0.4860    0.4565    0.4447    0.9218
    0.2311    0.8913    0.0185    0.6154    0.7382
    0.6068    0.7621    0.8214    0.7919    0.1763
C =
    0.2022    0.1034    0.0971    0.0946    0.1961
    0.0492    0.1896    0.0039    0.1309    0.1571
    0.1291    0.1621    0.1748    0.1685    0.0375

When a command generates more than one output, specify each output in square brackets to the left of the equals (=) sign. For example, the deal function distributes the values of each of its inputs to separate output variables:

[A, B, C] = deal([-12.3 4.89 -3.01], pi*1.46, diag(12:4:24))
A =
   -12.3000    4.8900   -3.0100
B =
    4.5867
C =
    12     0     0     0
     0    16     0     0
     0     0    20     0
     0     0     0    24

Other types of commands that can yield multiple outputs are assignments of structure and cell arrays, and calls to multiple-output functions. This example generates four outputs and assigns them to separate variables:

A(1).sym='H';  A(2).sym='He';  A(3).sym='Li';  A(4).sym='Be';
[hydrogen helium lithium beryllium] = A.sym

hydrogen =
   H
helium =
   He
lithium =
   Li
beryllium =
   Be

This example calls the fileparts function that returns several outputs, and assigns each output to a variable:

[path file exten] = ...
    fileparts('C:\matlab\work\strarray.mat')

path =
   C:\matlab\work
file =
   strarray
exten =
   .mat

For more information on assigning structure and cell arrays to multiple outputs, see Assigning Struct Values to Separate Variables and Assigning Cell Values to Separate Variables.

For information on assigning functions to multiple outputs, see Assigning Output Arguments.

Assigning Fewer Than the Full Number of Outputs

When assigning structure and cell arrays or when calling multiple-output functions, if you specify fewer output variables than there are return values, MATLAB assigns one return value to each output variable specified and discards the rest. Repeat the last two examples shown above, but specify fewer than the full number of outputs that are available:

[Symbol_1 Symbol_2] = A.symb
Symbol_1 =
   H
Symbol_2 =
   He


[path file] = fileparts('..\work\strarray.mat')
path =
   C:\matlab\work
file = 
   strarray

The deal function , however, does require that you specify the full number of output variables.

Commands that Call MATLAB Functions

When entering commands that call functions in MATLAB, you can use either of two syntaxes: command or function syntax. This is explained in the section Command vs. Function Syntax in the MATLAB Programming Fundamentals documentation.

You may also like...