01-10-2012, 11:24 AM
Basic Matlab Tutorial
MatlabTutorial.pdf (Size: 130.49 KB / Downloads: 65)
This tutorial is an introduction to Matlab (version 6.5) for people without much
programming, mathematical or Unix background. Matlab is an easy software package to
use even without much knowledge. That is what makes it so convenient. In this tutorial,
some basic and very useful functions are described. These should get you started. Having
understood a few basics, it will be pretty easy to expand your knowledge using Help, the
internet and manuals.
What is Matlab?
Matlab is a software program that allows you to do data manipulation and visualization,
calculations, math and programming. It can be used to do very simple as well as very
sophisticated tasks. We will start very simple.
Starting/quitting
To start Matlab, click on the ‘Start’ button on the left bottom of the screen, and then click
on ‘All Programs’, then ‘Math and Stats’, then ‘Matlab’. A window will pop up that will
consist of three smaller windows. On the right there will be a big window entitled
‘Command Window’. On the left there will be two windows, one entitled ‘Workspace’ and
another one ‘Command History’.
In addition, on the top there is a usual bar with ‘File’, ‘Edit’, etc. headings. You can use
these as you would in any other software (Word for example). Click on ‘File’ and look at
the available commands there. Do the same for all the other headings. Note that the last
heading is ‘Help’ (very useful!). Therefore, if you are stuck you know where to look.
Plotting basics
One thing we want to do to with functions is plot them. Matlab is a very good tool for
that. For example, to plot f as a function of x, type plot(x,f). A new window will come
up with a plot of f(x) as a function of x. Matlab has many features for plotting. We will
now learn a few of them. First of all we want to define our axes. This is very simple to do.
To define the x-axis, type xlabel(‘x’) in the Command window. Now switch to the
window with the figure. You will see a label on the x-axis. Let’s do the same for the yaxis.
Predictably, to do this you need to type ylabel(‘f(x)’). Now, if we want to put a
title on our graph the command it title(‘Function f’). As you can see, a lot of the
commands are quite intuitive.
Matlab as a tool for data analysis
What about looking at data in Matlab? Usually, data comes in tables. For example, you
could have some observation as a function of time, such as the average temperature as a
function of a month. This data will have two columns, one for the month and another for
the value of a temperature. Or you could have several replicates on an experiment where
the average weight of some organisms was observed in environments with different food
levels. This data can be recorded as a table with the number of rows equal to the number
of replicate experiments and the number of columns equal to the food levels tested.
A mathematical term for a table is a matrix. (From now on you can understand the term
matrix as table.) Matlab deals with matrices very well. Let’s create a matrix of ones in
Matlab. To do this, we need to know how many rows and columns we want in a matrix.
Suppose we want to create a matrix of ones with 2 rows and 3 columns.
Loading files
However, often you will have your data in a file in Excel or text format. The easiest
format to use with Matlab is a text format with extension .txt. (You can save your Excel
data in the text format by using ‘Save as…’ function under the File heading in the main
toolbar and picking the extension on the bottom of the dialog box.) To learn how to work
with files in Matlab, let’s make a text file with the data that we generated. Open Notepad
(to do this click on the Start button on the left bottom of the screen, then click on ‘All
Programs’, then ‘Accessories’, then ‘Notepad’). An empty Notepad window will come up.
Now, return to the Matlab window. Select the data that we generated and copy it (as you
would copy some text in Word). Now, paste it in the Notepad window and save the file as
data1.txt in the directory of your choice.
In order to load a data file into Matlab, we have to open the directory where the file is
located. To do this, look at the toolbar on top of the main Matlab window. There is a
Current directory name there followed by a button with three dots on it. Click that
button, it allows you to open a directory. Open the directory where your data file is
located. To make sure that you are in the right place, type ls a the prompt. This
command allows you to list (hence, ls) the files in the Matlab’s current directory. You
should see you file data1.txt in the list.