MATLAB Variables

Variables contain statically-typed data.


Declaration

Variables are declared using the equals sign (=). To suppress printing of the value assigned, complete the declaration with a semicolon (;).

>> m=3*5

m =

    15
>> m=3*5;

Variable names must start with a letter and must contain only letters, numbers, and underscores (_). Variable names are case-sensitive.

If an interactive command tries to declare a variable with an invalid name, the prompt is re-filled where the name is replaced with a suggested 'fix'.


Arrays and Matrices

All data are arrays. To declare a variable with more than 1 element, try:

r=[3 5]

This is a row vector. To instead declare a column vector, try:

c=[3; 5]

To declare a matrix, mix the two syntaxes:

M=[1 2; 3 4]

To concatenate arrays horizontally, try:

A=[c M]

To concatenate arrays vertically, try:

B=[r; M]


Workspace

Variables exist in a workspace. To clear the workspace, try clear.

To save all variables in the workspace to a file named like foo.mat, try save foo. To save a subset of variables instead, try save foo VARLIST.

To load a workspace from a file, try load foo. To load a subset of variables contained in that file, try load foo VARLIST.


CategoryRicottone