Commands in MATLAB

By clicking on the "Command Window", after the MATLAB prompt >>, a command can be typed. After having pushed the `return' key, the command is executed, and possibly the result appears on the screen. A command and its result looks like this:
>> a = 1 + 2 + 3
a =

The result of 1 + 2 + 3 is assigned to the variable a. You can now use this variable in succesive operations, such as:

>> b = 2*a + a/3
b =

In MATLAB, variables are introduced by assigning a value. Note, that the value can be numerical values, matrices (called arrays), or other types. You can use the variable, and later possibly assign a new value to the variable. It is not possible to perform calculations with variables that have not been assigned a value.
A command does not need to start with an assignment of the form variable =. In such a case, the result is automatically assigned to the variable ans. You can then use ans further.

>> 4*a + 1
ans =
>> ans*ans
ans =

If you now want to know what the value of a is, the following command suffices:

>> a
a =

Normally, MATLAB displays the output below the command. The output is suppressed by ending the command with a semicolon. After having entered the command $ s = 1 + 2$ , the screen looks as follows

>> s = 1 + 2;

The value 3 has been assigned to the variable s. If you want to know the value of this variable, you can either look in the " Workspace" window on the top left of your MATLAB window, or request the value of s as follows

>> s
s =

If a command is longer than one line, you can end the line with three dots, and then press the `return' key. You can then continue on the following line. After completing the command, the screen looks as follows:

>> s = 1 + 2 + ...
3 + 4
s =

Mathematical expressions

You cannot enter mathematical expressions literally. The Tables 1.1-1.2 make clear how mathematical expressions can be entered. The variables in these tables are to be interpreted as numbers.

Table 1.1: Standard operations
MATLAB Standard
a+b $ a+b$
a-b $ a-b$
a*b $ ab$
a/b $ \frac{a}{b}$
a^b $ a^b$

Table 1.2: Mathematical operations
MATLAB Standard
sin(x) $ \sin(x)$
sqrt(x) $ \sqrt{x}$
cos(x) $ \cos(x)$
exp(x) $ e^x$
tan(x) $ \tan(x)$
log(x) $ \ln(x)$
asin(x) $ \sin^{-1}(x)$
log10(x) $ \log_{10}(x)$
acos(x) $ \cos^{-1}(x)$
abs(x) $ \vert x\vert$ ,i.e. absolute value of $ x$
atan(x) $ \tan^{-1}(x)$
sign(x) sign($ x$ )
mean(x) mean($ x$ )
std(x) standard deviation min(x)
max(x) max($ x$ )
rand(x,y) returns $ x\times y$ array of random numbers, distributed uniformly in [0,1] *
randn(x,y) returns $ x\times y$ array of random numbers, distributed normally with mean 0 and variance 1*
round(x) Rounding to nearest integer
floor(x) Rounding to smaller integer
ceil(x) Rounding to larger integer
* Omitting y yields $ x\times x$ array and
omitting x,y yields a single number

Remark: The order of operations in MATLAB is the standard order: first raising to a power, then multiplication and division, and after that addition and subtraction. To deviate from this sequence, parentheses `(' and `)' need to be used to define the order of calculations, e.g. $ \frac{1}{\exp(3)+1}$ is obtained by running 1/(exp(3)+1).


A (long) MATLAB calculation can be interrupted with `ctrl-c'. After this, a new prompt appears and new commands can be entered. This interruption is especially useful when a programmed loop is running that is corrupt.

Incomplete commands

If a command is incomplete or invalid and you press the `return' key, there are two possibilities:


The name of a variable in MATLAB has to start with a letter. After that, the name can consist of an arbitrary number of letters, numbers or symbols like '_' and '-'. MATLAB does distinguish between upper and lower case letters.
MATLAB uses some special variables, shown in Table 1.3.

Table 1.3: Predefined variables in MATLAB
Variable Explanation
ans This variable contains the result of the last calculation that has not been assigned to another variable.
eps The value of this variable is approximately $ 2.2204 \cdot 10^{-16}$ and is being used internally by MATLAB. This number is the default computation accuracy of MATLAB which is used to round of all numbers for storage in the computer memory. Do not confuse eps with the number $ e$ , because $ e = \textup{exp}(1)= 2.7182...$
i or j The complex number $ i$ with the property that $ i^2 = -1$ .
pi 3.1415...
Inf This is the value infinity. When one divides 1 by 0, 1/0, the result will be Inf.
NaN This variable is a representation of `Not a Number'. A calculation with NaN always results in NaN. The command 0/0 produces NaN.

Warning: in principle, it is possible to assign a value to the internal MATLAB variables presented in Table 1.3. This can influence your calculations. Therefore, it is wise never to assign a value to an internal MATLAB variable. Commands like >>pi=10 should be avoided at all times! If by accident you have given a different value to an internal MATLAB variable, you can remove this value by using clear or the workspace browser.

On the top right of the default MATLAB window, you will find the "Workspace" window where all variables in use are listed. The browser also indicates to which class the variables belong. If necessary, you can remove variables by using the browser. The special variables above will not be listed
Some other important commands for the management of variables are given in Table 1.4.

Table 1.4: Important commands.
Command Explanation
who Gives a list of the variables in use.
whos Gives a list of the variables in use as well as some extra information.
clear Removes all variables.
clear x y Removes the variables $ x$ and $ y$ .

Previous      Next      Up      Contents

Esteur 2010-03-22