You are on page 1of 13

1

1.0 MATLAB - Introduction



MATLAB stands for 'MATrix LABoratory'. Its basic data element is matrix. MATLAB
is powerful in numerical computation and visualization. With its matrix-based
techniques problems can be solved easily without having to write detailed program
codes as in traditional languages like C, Basic and Fortran. Under its easy to use
environment, MATLAB has the capability to perform numerical analysis, matrix
computation, signal processing, and plant controls. MATLAB also has a superb
graphical display feature.

1.1 MATLAB Version

The lessons in this book will be carried out in MATLAB Version 5.2. The earlier
versions are MATLAB 4.2 and MATLAB 5.1.

1.2 Access to MATLAB

There are several ways to open MATLAB.
a) From the Start icon at the bottom of the screen of Windows.



After clicking the Start icon, you will then need to select MATLAB.



2

b) You can also start MATLAB through Microsoft Explorer.
From the Start icon, select Programs, and then Microsoft Explorer. Go to MATLAB
folder. Choose its subdirectory Bin. Double click on MATLAB to start the
application.






c) Or, the easiest way is to click on the Shortcut Icon .




1.3 Description of MATLAB Window Screen

Once you are in MATLAB Window Screen, the first thing you will notice is the
MATLAB command prompt ().


Double click on MATLAB icon

3

The command prompt is where you can write down the MATLAB commands (similar to
the Dos prompt that receives Dos commands).

The top part on the MATLAB Window contains the Menu. Several choices can be
picked from the Menu.



We will discuss how, where and when to use the Menu later.


1.4 MATLAB command, exit or quit

The first MATLAB command we will learn is the exit command. This command will
cause the termination of the MATLAB application. quit will give the same results where
MATLAB application will be closed.





4

1.5 Change Directory

Change directory command in MATLAB is similar to Dos command. The command is
cd. The change directory command is useful because there will be many occasions
where you would like to work in a certain directory only.
For instance, if you want to have all your saved files written on your diskette in 'a' drive,
you will write:

cd a:\

This will direct your saved applications to 'a' drive.
If your a directory contains a folder called MEMB203, to change to the directory, you
will write:

cd a:\MEMB203

If there is a subdirectory under MEMB203 folder called Semester I, to change to the
directory, you will write:

cd(a:\MEMB203\Semester I)

Notice that you have put the path of the directory in a string.


1.6 List Files in Directory

To list out the names of the files in a directory, the following command can be used:

>> ls



or, you can write:
5



1.7 Several MATLAB Commands

MATLAB commands or functions given in this book will be written in bold face.
MATLAB commands must be written in lower case. MATLAB is case sensitive, thus
writing in small and capital letter will make a difference.

1.7.1 diary

This command acts like a diary. It will record all the characters you type. The recording
will be saved in a file you assigned. The diary will also record the output generated by
MATLAB. In fact all the things shown on the screen will be recorded.
To initiate the diary, type diary follows with the file name:

diary lesson1.dia

This will start the recording. The extension is purposely picked as .dia, which stands for
diary.

Example
Lets say you have typed the followings at MATLAB command prompt right after
initiating
diary lesson1.dia

6


When you open the lesson1.dia file, you can see the followings:



Basically, it says that whatever echo on the MATLAB command screen, will be saved in
the file specified.

7
Why do you need the diary?
There are several advantages in using diary:
a) You can recall all the commands you have typed.
b) The file can be edited. You can delete all the commands which gave errors, and keep
all the commands that are working.
c) Diary can be regarded as a test file. You can try commands that you are not sure, and
check the results. If you don't keep a diary then it will be difficult to check what you
have done.


1.7.2 help

You can use help command whenever you want to get more information on certain
command, just type help followed by the MATLAB command you want to look for.
For example, if you want to know more about the command save, you can type:

help save



A description about the command will be displayed. This information can serve as a
guideline on how to use the command.


1.7.3 lookfor

Lookfor command is used together with a keyword to do a searching. This keyword can
be a MATLAB command or any other English word that you feel can give you more
information on certain topic that you are looking for.
Lookfor command will go through every command available in MATLAB, and will
check whether the keyword that you have listed is in the database. If found then the
commands will be displayed. Lookfor is different from Help because you use Help only
with a MATLAB command. You must know what command you want to look for before
8
you can use Help. But, if you don't have any clue the right command to be used, then
you can always use Lookfor together with a keyword.
For instance, let say you don't know what is the command used to record a particular
MATLAB session to a file, then you can always search using a keyword. The keyword
may be words like save, keep, record, and session.
Let try the keyword save.



Above are some of the commands found which contain the word 'save'. Immediately,
you can see that the command diary can be used to record your session to a file.


1.7.4 Ctrl-c

Ctrl-c is not a MATLAB command, instead it is used to interrupt the ongoing running
process. Press the key Ctrl and the letter 'c' simultaneously to execute the action. When
to use it? There are many occasions where this action is useful. For instance while
searching using the command lookfor, MATLAB will go through all the commands in
the library, and this will take quite a while. While searching MATLAB will list out on
the screen, the commands that match with the keyword. If the command that you are
looking for is already listed on the screen, then you can use Ctrl-c to stop or interrupt
MATLAB from continuing with the search.
Another occasion is when you are running a MATLAB program. If there is a mistake in
the program and you want the program to be stopped then you can always use Ctrl-c to
interrupt the process.

9
1.8 Workspace Commands

When you open MATLAB, the first thing you will be introduced is its prompt (>>).
MATLAB commands can be entered at this prompt, and the results will be displayed on
the next line.

There are several commands that are useful when you navigate through MATLAB, and
those commands are described below. Note that probably you won't know how to utilize
the commands at this stage, but you can always come back to this page after going
through more detail explanations and examples in the coming lessons.


1.8.1 save

If you would like to save the variables that are currently in the workspace memory, type:

save var1

This will save all the variables that are currently in the memory to a file named var1.
You can select any names for the file, but it must follow the acceptable dos format. The
file will be saved with an extension .mat. So the above file will be: var1.mat

Example
First type the command who. who will list all the variables in the current workspace.
cd a:\ means that you would change the directory to a drive.

cd a:\
who



Example above shows that the variables that are currently in the workspace are a, ans, x,
xunit, and y. You would like to save these variables in a file called var1.
10

First list down the files in the current directory:
ls


so, a drive only contains two files, convert.m and lesson1.dia

Now type the save command, and then list down the files using ls.

save var1
ls


Notice that a new file called var1.mat has been added to the current directory. When you
use the save command MATLAB will automatically give an extension .mat to the file.


1.8.2 clear variables

This command will clear all the variables from the memory. Meaning that the variables
a, ans, x, xunit, and y currently reside in the memory will be cleared.

clear variables
who

When you execute the above two commands, there will be no variables shown because
all the variables have been cleared.


1.8.3 load

load command will load workspace variables from the disk. In the above example, since
we have saved the variables in the var1.mat file, we can load them using the load
command.
11
load var1
who


The variables have been loaded back to the memory using the load command.


1.8.4 clear

Clear will remove all variables from the workspace. For instance if your workspace
contains variables x, y and z , then by issuing the command clear, all of these variables
will be wiped out from the workspace.
But, if you just intend to clear only one of the variables, then you can issue a command
like:

clear x

Variable x will be cleared from the workspace. The other two variables will still stay
intact.
Another related command is clear all. This will remove all the variables, functions, and
MEX links from the workspace.


1.8.5 who, whos

To know the variables that are currently in the work space type:

who

Who will list out the names of the variables. But, if you need more information, then you
can key in:


12
whos

whos will give the names of the variables, their sizes, number of elements in each
variable and the number of bytes used by the variables. It also species whether the
density of the variables and whether they are complex number or not.


1.8.6 clc
This is a command to clear the command window. Sometimes the MATLAB command
window is filled with all the descriptions and output printed on the screen. To have a
fresh clean window, then issue the clc command. Everything you see on the screen will
be cleared and you can start with a fresh window. This command only clear the screen, it
does not clear the variables that are in the workspace.


1.8.7 clf

clf stands for clear current figure and it will remove the display on the current graphic
window.


1.8.8 ans

If you do not assign a variable to an expression the results of the expression will be kept
in ans.
For instance:
a = 1 + 3
a =
4
The result, 4, is assigned to a. But, if you only type:
1+3
ans =
4
Since, you do not assign a variable to the expression 1+3, the result is kept in a variable
named ans.
13
1.8.9 %

To write a remark in MATLAB you can use '%' symbol, followed by the remark you
would like to write.

Example
% This will sum up all the numbers in the matrix

MATLAB will ignore the statement made after the % symbol.

You might also like