# First Steps with Matlab

## Contents

Matlab is powerful software package in order to visualise data and to perform numerical computations and simulations. Basically, Matlab is commandline based, but can be extended by graphical user interfaces. Due to the command based nature of Matlab, it is neccessary to learn some fundamental commands. Often used sequences of commands can be stored either as Matlab scripts or as Matlab functions. Although many tasks, like data import and export, can be done by clicking on some menu entries in the Matlab desktop environment, we will focus on the commands because they are necessary to know for an efficient application of Matlab.

## Variables and matrix manipulation

Matlab stores values in variables which can be scalars, vectors or matrices:

a scalar

x = 1;

a raw vector

x = [7 2 9]

x = 7 2 9

a column vector

y = [7; 2; 9]

y = 7 2 9

a raw vector of a series with elements zero to nine

y = 0:9

y = 0 1 2 3 4 5 6 7 8 9

a matrix

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

X = 7 2 9 4 3 1 6 5 8

The advantage of Matlab is that we do not need to specify before, what kind of data we would assign to a variable - Matlab will do it automatically. We can also reassign an already existing variable of a scalar type with a matrix.

The semicolon at the end of a line suppresses the output of the command or variable assignement. If we need to check which variables are already in the Matlab workspace we can type

whos

Name Size Bytes Class Attributes X 3x3 72 double ans 1x32 64 char x 1x3 24 double y 1x10 80 double

This command lists all current variables, their size and data type (class).

Matlab knows several commands for matrix manipulation. The important ones for our purposes are to tranpose a matrix, to force a matrix to a column vector, to remove and to access on certain values in a matrix.

Any matrix

X = [7 2 9; 4 3 0; 6 5 8]

X = 7 2 9 4 3 0 6 5 8

transpose the matrix

X'

ans = 7 4 6 2 3 5 9 0 8

force matrix to a column vector

X(:)

ans = 7 4 6 2 3 5 9 0 8

Access to values:

value in first line, second column

X(1,2)

ans = 2

all values in the second column

X(:,2)

ans = 2 3 5

the 6th value, corresponding with value in the 2nd line, 3rd column

X(6)

ans = 5

position of the value 9 in the matrix

find(X == 9)

ans = 7

remove the second raw

X(2,:) = []

X = 7 2 9 6 5 8

This is also useful or if we need to remove a certain value during a computation, like the zero for the calculation of the logarithm

log(y(y ~= 0))

ans = 0 0.6931 1.0986 1.3863 1.6094 1.7918 1.9459 2.0794 2.1972

Further commands allow to flip and rotate a matrix, like `rot90`, `flipud` or `fliplr`, to exchange the dimensions with `permute`, or to remove singleton dimensions with `squeeze`.

For numerical calculations, the common arithmetic operators are available, like `+` or `*`. Note that for matrix operations, a preceeding `.` has to to be added to the operator.

x = [2 4 1]; y = [1 2 2]; x .* y

ans = 2 8 2

Missing values can be denoted as `NaN`

x(1,3) = NaN

x = 2 4 NaN

Note that this requires special attention when applying functions to such vector containing NaNs. To remove NaNs from a vector
`x`, we can use

x(find(isnan(x))) = []

x = 2 4

In order to display values of a variable, there are different methods in Matlab. The simplest one is to call the variable name on the command line

x

x = 2 4

In order to combine the output of a variable with text, the command `display` is appropriate.

```
disp(['Vector x contains: ', num2str(x)])
```

Vector x contains: 2 4

The command `num2str` converts number to a string, which can be concatenated with some text.

A more advanced possibility is the `sprintf` command, which allows formatted output.

```
sprintf('Vector x contains: %6.2f and %02d', x)
```

ans = Vector x contains: 2.00 and 04

The format string (first argument) corresponds to the C language and can be combined with text. The format conversion specifications start with the character %.

## Data import and plotting

Usually, the first step is to import data from an external file and to plot them. Here we assume that our data are stored
in an ASCII file. Matlab knows different commands to import data from ASCII files. The most simple is the command `load`:

```
x = load('ecg.dat');
```

Any heading lines have to be commented out by the % sign. Column arranged data will be imported as a column vector. If there
are more than one column of data in the file, the resulting Matlab variable will be a corresponding matrix with the same number
of columns, as shown in the file `cycles.dat`, where the first column presents the time scale of the data

```
y = load('cycles.dat');
```

Alternative methods with more flexibility are `textread`, `dlmread` or lower level file operation methods (`fopen`, `fread`, `fclose`).

In order to plot the data, Matlab knows the command `plot`

plot(x)

A small section of the data, e. g., only the epoch [200 400], can be plotted by calling

plot(x(200:400))

The plot command knows different optional arguments specifying, e. g. colour, line style and line width. The simple version for plotting, e. g., in red and using crosses is

```
plot(x,'rx')
```

The exact version for doing the same is

plot(x,'color',[1 0 0], 'linestyle', 'none', 'marker', 'x')

which allows more settings or combinations of settings, like linewidth or a particular line colour

plot(x,'linewidth', 3, 'color', [.9 .6 .1])

The limits of the plot can be fixed from the commandline with the commands

xlim([600 700]) ylim([-.5 .5])

or by using

axis([600 700 -.5 .5])

for both axes together.

In the file `cycles.dat`, a time scale was given by the first column. In order to use this time scale in the plot, we use

plot(y(:,1), y(:,2))

The axes and the plot itself can be labeled with the commands

xlabel('Time') ylabel('x') title('Data from file cycles.dat')

To plot more than one data series in one plot, we have to tell Matlab that it should keep the old content in the current axes (and not remove it) by

hold on plot(y(:,1), abs(y(:,2)), 'r')

Alternatively we can plot both data series together by using the `plot` command

clf plot(y(:,1), y(:,2), 'k--', y(:,1), abs(y(:,2)), 'r:')

In the case of two data series containing very different amplitudes, it is better to have to separate y-axis. For this purpose,
Matlab knows the command `plotyy`

clf i = 0:.01:10*pi; x = cos(i); z = 5 * sin(i) .^ 2; plotyy(i, x, i, z)

Other plots are stairstep plots

stairs(y(:,2))

discrete sequence plots

stem(y(:,2))

and bar plots

bar(y(:,2))

Logarithmic plots can be performed by the commands `semilogx`, `semilogy` and `loglog`

```
semilogy(x) % plot the y-axis logarithmicaly
```

Now let us consider a data set consisting of four columns, where the first is a time scale and the last three columns are the x-, y- and z-component of three-dimensional system:

x = load('roessler.dat'); t = x(:,1); % assign a new variable with the time scale x(:,1) = []; % remove the time scale from variable x

To plot the three-dimensional system we use

figure(2) plot3(x(:,1), x(:,2), x(:,3)) box on, grid on

Matlab knows further commands to enhance the plot, like `grid on`, `grid off` or `box on`, `box off`. The figure command opens a new figure window designated with the number 2. By calling `figure(1)` and `figure(2)` we can switch between the figures 1 and 2. The axes view can be changed with

view([-33 20])

For the presentation of spatial data, contour or surface plots are appropriate. Let us consider a function of two variables:

[X, Y] = meshgrid(-8:.5:8); R = sqrt(X.^2 + Y.^2) + eps; Z = sin(R) ./ R;

The meshgrid function transforms the domain specified by a single vector or two vectors x and y into matrices X and Y for use in evaluating functions of two variables. The rows of X are copies of the vector x and the columns of Y are copies of the vector y.

A filled contourplot is displayed by

contourf(X, Y, Z,20)

a checkerboard plot of the matrix `z` by

pcolor(X, Y, Z)

a simple mesh plot by

mesh(X, Y, Z, 'EdgeColor', 'black')

and a surface plot by

surf(X, Y, Z)

Surface plots can be enhanced by changing the colourmap, the shading and ligthing mode, switching on the light and adding a colorbar

colormap(hot) shading interp lighting phong, light colorbar

In order to arrange several plots in one figure, the command `subplot` divides the figure window into several panels.

subplot(2, 1, 1) % divides the figure in 2 rows, 1 column and creates the first plot plot(x) subplot(2, 1, 2) % creates the second plot plot(y(:,2))

The command `clf` clears the current figure window; the command `cla` clears only the content of a plot, but maintains the position of the current axes.

## Data export

Single Matlab variables or all variables in the Matlab workspace can be saved in a file with the command `save`. The command

save testfile x

stores the variable x in the file `testfile.mat` which is in the Matlab binary format. In order to save the data in an ASCII format, we have to add the option `-ASCII`

save testfile.dat x -ASCII

Analogous to the lower level file operations for data import, Matlab can also write data using `fopen`, `fprintf` and `fclose`. This is especially useful when a predefined output format is desired.

## Scripts and functions

Often used sequences of commands can be stored as a script in a text file with the extension `.m`. This script can be called from the Matlab commandline by calling the name of this file (but without the extension `.m`). The commands contained in this script are then applied on the variables in the current Matlab workspace.

Let us create a file `myplot.m` with a text editor (or the Matlab editor)

```
edit myplot.m
```

and include the following lines: `subplot(3,1,1), plot(t,x(:,1))` `subplot(3,1,2), plot(t,x(:,2))` `subplot(3,1,3), plot(t,x(:,3))` `xlabel('Time')` Now we load again the data from the file `roessler.dat`

x = load('roessler.dat'); t = x(:,1); % assign a new variable with the time scale x(:,1) = []; % remove the time scale from variable x

and call on the commandline

myplot

This script uses the variables `x` and `t` from the workspace for the plot.

However, we cannot use such a script like a function, where we can commit some arguments, e. g., for our plotting example
the name of the file. For this purpose, we need to change the script to a function. A Matlab function is very similar to a
script file (it has the same extension and uses the same commands). But we have to add a special first line to this file:
`function myplot2(filename)`

function myplot2(filename) x = load(filename); t = x(:,1); % assign a new variable with the time scale x(:,1) = []; % remove the time scale from variable x subplot(3,1,1), plot(t,x(:,1)) subplot(3,1,2), plot(t,x(:,2)) subplot(3,1,3), plot(t,x(:,3)) xlabel('Time')|

Now we can call this function with an argument specifying the filename:

clear % clear the Matlab workspace myplot2('roessler.dat') myplot2('lorenz.dat')

The big difference to a script is that a function does not access to the variables in the current Matlab workspace. A function has its own workspace. If there are some variables on the Matlab workspace, which should be accessible by the function, we would need to commit them as arguments. A function can also have some output.

The following example `stat.m` computes the mean and the standard deviation of a vector.

function [a, b] = stat(x)| % STAT Mean and standard deviation of a vector.| a = mean(x);| b = std(x);|

Let us apply this function `stat` on some random numbers. We assign the variables `m` and `s` with the output of this function, corresponding with the mean and standard deviation.

```
rand('seed',1)
x = rand(1000,1);
[m s] = stat(x)
```

m = 0.4820 s = 0.2824

The command `rand` creates a random number from a uniform distribution, `randn` creates a random number from a normal distribution. The optional arguments specify the size of the result matrix, e. g.,
`rand(10,4)` creates a 10 x 4 matrix of random numbers.

Matlab knows a lot of helpful commands for data manipulation. Let us summarise some important here

x = randn(4,7); y = randn(10);

length of vector y

length(y)

ans = 10

size of matrix(x)

size(x)

ans = 4 7

number of elements in matrix x

numel(x)

ans = 28

for i = 0:10 % a loop if i > 5 % a statement condition disp(num2str((10-i) * 2)) else disp(num2str(i * 2)) end end

0 2 4 6 8 10 8 6 4 2 0

## Vectorisation

Using vectorisation, Matlab computations can be enhanced. Vectorisation replaces loops:

y = []; for x = 0:.1:4*pi y = [y sin(x)]; end x = 0:.1:4*pi; y2 = sin(x);

Vectorised calculations perform much faster than corresponding calculations using loops. Note that more complex calculations need a little bit more effort to vectorise. Assume we would like to compute the difference between each pair of values in two vectors

```
rand('seed',1);
x = rand(500,1);
y = rand(500,1);
```

Without vectorisation we would calculate the distance matrix with two loops

for i = 1:length(x) for j = 1:length(y) d(i,j) = abs(x(i) - y(j)); end end

Using vectorisation we have first to create new vectors containing repetitions of the original values, but arranged in that way that we get all pairs between the components of the both vectors

x_vec = repmat(x, length(y), 1); y_vec = reshape(repmat(y, 1, length(x))', length(x) * length(y), 1);

Using the command `repmat` we replicate a vector and using the command reshape we re-arranges a matrix to a single-column vector.

The distance matrix is first a vector

d2 = abs(x_vec - y_vec);

but using `reshape` we get it in the matrix form

d2 = reshape(d, length(x), length(y));

Alternatively we can also use meshgrid in order to create pairs between the components of the both vectors

[x_mat y_mat] = meshgrid(x,y); d3 = abs(x_mat - y_mat); d3 = d3';

(Here we need to transpose the result because the `meshgrid` is actually designed to form the basis of 3D plots and yields transposed matrices.)

## Exercise

1. Plot the data from the file `ecg.dat`.

2. Create a 1000 x 9 matrix of normally distributed random numbers and save them in ASCII format in a file called `random_numbers.dat`.

3. Create a script which plots each of the columns of the file `random_numbers.dat` as a separate plot in the same figure (3 x 3 plots).

4. Consider the function $x e^{-x^2 - y^2}$ and create a contour, mesh and surface plot within the intervals x = y = [-2 2].