MATLAB is both a software and a programming language.

It is the most widely used tool for analysing scientific data. You can use MATLAB for visualising data, mathematical modelling, running simulations, etc. etc.

This is a gentle introduction to very basic MATLAB commands. You are encouraged to run the commands while reading this document.

To follow this practical, it is desirable that you have MATLAB installed on your computer. If you do not have MATLAB installed, you can alternatively use OCTAVE, which is a free software that is very similar to MATLAB in its basic syntax. You do not have to install OCTAVE on your computer. Instead, you can run the online version at www.octave-online.net (obviously this means you need to be connected to the internet...)

Start by opening MATLAB (the exact way to do this will depend on which type of computer you have, but generally if MATLAB is installed there should be an icon you can double-click on. Alternatively, you can use OCTAVE online at www.octave-online.net. If you choose this option, you can optionally sign in at the website to get more options out of OCTAVE.

Once you have opened MATLAB, you should see an interface that looks like this:

We will mainly be using the 'Command window' which is a window where you can type and execute commands. In OCTAVE, this is called 'Command Prompt'.

In MATLAB/OCTAVE you can manipulate *variables*. These are generally scalar quantities (i.e. real numbers), or *vectors*, or *matrices*. Other types of variables are also dealt with in MATLAB (e.g. text).

A simple way to define a variable in MATLAB is to assign it a value. For example (type into the command window and press Enter):

a=5

This will create a variable called 'a' which takes value 5. You can see the name of the variable appear in the 'Workspace'. This means MATLAB will remember that 'a' is a variable, and that for now it has value 5, which you can change to some other value if need be.

Notice that by typing the above, MATLAB will both store a variable called 'a' which has value '5', but will also display 'a' in the command window. If you do not want MATLAB to display the operation you are asking it to do, just add a semicolon at the end of the command:

a=5;

A vector is a list of numbers, for example:

v=[1 3 5 0 -1 0.7]

The squared brackets are used to indicate to MATLAB that the numbers form a vector. There is a distinction between row vectors and column vectors in MATLAB. A column vector can be defined using either of these:

w=[1;3;5;0;-1;0.7] w=[1 3 5 0 -1 0.7]'

The semicolons means the numbers are stacked on top of each other, whereas the single quote takes a row vector and flips it to a column vector. You can check the sizes of v and w:

size(v) size(w)

Standard mathematical operators are available in MATLAB. The most common operations/symbols being:

+ addition - substraction * multiplication / division ^ power

But there is a twist: the above operators can be used on scalars (numbers). But for vectors, the same operations are done with these symblols instead:

+ addition - substraction .* multiplication ./ division .^ power

As you can see, the only difference is that we need to add a '.' just before the last 3 operators.

MATLAB allows you to perform a VAST number of mathematical operations on variables. Here are a few examples:

v+a add vectors 'a' and 'v' 2*v+v.^2 multiply v by 2 and add its square sin(v) sine of v cos(v) cosine of v tan(v) tangent of v

We are not going to go through all the possible mathematics in MATLAB, we can learn them as we go along doing other things. For now, just remember that MATLAB allows you to manipulate variables using mathematical operations.

MATLAB is very useful for graphics. Let's do a simple example. We will plot the function `sin(x)`

for -π<x<π.

First, define a vector of numbers between -π and π:

x=-pi:0.01:pi;

Notice the literal use of the word 'pi' in the above command. We can do this because 'pi' is an internal MATLAB variable that takes value 3.1415926... (it will not recognise other greek letters!).

This command creates a vector of numbers between -π and π with a step of 0.01 (meaning the space between consecutive entries in the vector are 0.01).

Note how many entries the vector x has:

size(x)

Another way to defined x is, instead of choosing the interval between two consecutive values, would be to choose the number of entries (or size) of x:

x=linspace(-pi,pi,1000);

In this case the number of entries in x is exactly 1000 (check by typing `size(x)`

).

Now we define a vector that calculates the sine of x:

y=sin(x);

We are now ready to draw the sin(x) function. We will use the `plot`

command to draw the function:

plot(x,y);

You should be able to see something like this:

Let's see if we can make this figure look a little bit prettier. First, we add labels for the x and y axis (it is good scientific practice to always have labels on axes). We also (optionally) set the fontsize a bit bigger than the default.

xlabel('x','fontsize',20) ylabel('sin(x)','fontsize',20)

Now let's add a grid:

grid on

Finally, we can make the axes a little thicker and the background white. For that we will use the following commands:

set(gca,'linewidth',2) set(gcf,'color','w')

You should now be able to see something like:

Now try to plot other functions (cosine, tangent, etc.) in the
interval `[-5π,5π]`

. Also have a look to see how the plot command works using the `help`

:

help plot

All MATLAB functions come with a help section so you can learn how to use them. The tricky bit is to find out what the commands are called so that you can read the help! My strategy is to use Google...

Let's do some more advanced drawing. This time we will draw a function of 2 variables `f(x,y)=sin(x`

. We start by defining x and y, for example as vectors between -2π/5 and 2π/5:
^{2}+y^{2})

x=linspace(-2*pi/5,2*pi/5,100); y=linspace(-2*pi/5,2*pi/5,100);

Now the tricky bit: we want to draw a function f(x,y) for every pair of values x and y in the interval [-2π 2π]. For that, we need to turn the vectors x and y into a grid with the command:

[x,y]=meshgrid(x,y);

This turns x and y into *matrices*. Don't worry about this too much for now. Let's create the function (we will store its values in a variable called z):

z=sin(x.^2+y.^2);

Note the "`.^2`

" is used for squaring a variable. We are
now ready for plotting the function. There are several possible commands that will produce different types of plot. The most fun is `surf`

, which represents the function as a surface:

surf(x,y,z);

Now if you add axis labels (as we have done earlier), you should be able to see something like:

Now let's play with some real world data! I like to use Oxford weather data, which I originally found here: http://www.metoffice.gov.uk. You can download a textfile of a cleaned up version from this link: OxfordWeather (right-click on the link and save on the disk).

Once you downloaded the weather data file, change the matlab folder to where you've saved the file and type:

data=load('OxfordWeather.txt');

If you are using OCTAVE, you may need to register (should take < 1 minute) before you can load files from disk.

This will create a variable called 'data', which is a table of numbers (996 rows and 7 columns). You can check the size of the data table with `size(data)`

.

The different columns of numbers represent data collected every year from 1929 to 2011, and the data is listed in the following order:

1st column = year 2nd column = month (1 to 12) 3rd column = mean daily maximum temperature (in degrees) 4th column = mean daily minimum temperature (in degrees) 5th column = days of air frost 6th column = total rainfall (in mm) 7th column = total sunshine duration (hours)

Now let's make a simple plot of maximum temperature as a function of month of the year. Begin by creating one variable for month and one variable for maximum temperature:

month = data(:,2); maxTemp = data(:,3);

In the command above, we use the '`:`

' to mean 'all
rows of the data table', and the number represents which column we
want to extract. If for example we only want rows 1 to 10 and column
2, we would write e.g.: `month=data(1:10,2)`

. This type of syntax is called 'indexing' in MATLAB.

Now let's do a quick plot of the max temperature data:

plot(month,maxTemp,'o');

The symbol `'o'`

means draw one little circle per data point (instead of the default, which is to draw lines). You can look at the help for the plot command to see what other options are available.

Now let's not forget the axes labels like good scientists do!

xlabel('Month') ylabel('Maximum temperature (degrees Celcius)');

Two things to notice in the plot: (1) The temperature rises from the first month to the middle, then drops again; this is because Oxford is in the northern hemisphere (duh); and (2) within each month of the year, there are variations from one year to the other, so the data points coming from different years (between 1929 and 2011) do not overlap exactly. In fact there can be as much as 10 degree fluctuations!

It is more informative to have names of months in the x-axis instead of numbers. Let's do that:

First, define a variable that contains the names of the 12 months. We will use a 'cell array', instead of a vector (by using curly brackets {} instead of square brackets []). This is because the names of months are text and not numbers:

names = {'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec'};

And to include these into the figure, we use the following syntax:

set(gca,'xtick',1:12,'xticklabel',names);

You should now be able to see something like this:

Ok let's plot something slightly more complicated (but more fun). For this we need another two variables: year and rain:

rain = data(:,6); year = data(:,1);

We want to plot the yearly rainfall in the years 2010 and 2011 (in different colours so we can compare them). Let's begin. The following code has two new pieces of syntax. Run the code and then read the explanation that follows.

x = month(year==2010); y = rain(year==2010); plot(x,y,'bo-'); hold on x = month(year==2011); y = rain(year==2011); plot(x,y,'ro-');

Notice the use of 'year==2010' and 'year==2011'. This is another (very useful) way of doing 'indexing' in MATLAB, using the logical operator '=='. This will select the entries in 'rain' and 'month' that correspond to when the 'year' variable is equal to 2010 or 2011.

The second piece of syntax is the use of '`hold on`

'. Without it, the second plot would have erased the first one, so we use this syntax to superimpose different plots. You only need to use `hold on`

once and then add as many plots as you want. If you want to revert back to erasing current plot, you can use `hold off`

.

Now let's add axis labels and replace the x-axis numbers with month names as before:

set(gca,'xtick',1:12,'xticklabel',names); xlabel('Month','fontsize',20) ylabel('Rainfall (mm)','fontsize',20);

Finally, let's add a legend:

legend({'2010' '2011'})

Looks like August was particularly miserable in 2010... But April 2011 was great!

In order to reinforce the few lessons learnt in this practical, we will finish with an exercise.

Using the Oxford weather data, can you extract the maximum daily temperature of the years 1930, 1960, 1990, and 2010; plot them as a function of the month of year, make the plot pretty, and save it as a JPEG image. Click here for example answer

The End.

Created by Saad Jbabdi (saad@fmrib.ox.ac.uk)