MATLAB Basics
(last updated 9/9/99)
The information in this tutorial is located in the MATLAB manual. Any page or section numbers refer to the following:
The Student Edition of MATLAB, Version 5, The MATH WORKS Inc. Prentice-Hall, 1997.
__________________________________________________________________________________________________
The following topics are covered in this tutorial;
Using The MATLAB Command Window
Matlab's Interaction With the Computer Memory
Scientific Features / Common Math Functions
=======================================================================================
Double click on the MATLAB icon. The MATLAB Command Window will appear and the MATLAB prompt >> is displayed.
=======================================================================================
You can exit MATLAB in three ways:
Using The MATLAB Command Window - Section 1.2, 3.1
This is the window with the MATLAB double prompt, >>. All MATLAB commands are executed from this window. When an m-file is executed from the Command Window, it is the same as if the individual commands had been typed at the double prompt, >>.
Suppressing Display
Putting a semi-colon at the end of any line of MATLAB
code will cause the calculation to be performed, but the answer will not
be displayed. The answer is still stored in memory. This is particualrly
useful inside of m-files, functions and loops that are accessed often during
a program.
Retrieving Previous Commands
While in the MATLAB workspace, you can move back and
forth through the list of commands you have used previously by using the
up and down arrow keys. The left and right arrow keys move within a command.
This makes for an easy way to repeat a command or edit a previous command.
Hitting return executes the command again. To view previous portions of
the workspace use the sliding button or arrows on the right side of the
workspace screen, not the arrow keys.
Clearing the Screen
The MATLAB Workspace screen can be cleared by clicking
on EDIT / CLEAR SESSION, or by typing clc at the workspace prompt. This
does not clear the variables in memory.
Multiple Commands
Multiple commands can be placed on one line if they are
separated by commas or semicolons. This is not recommended because
it is harder to read and debug.
>> a =4, b = 6; c = 2
a =
4
c =
2
Note that 'b' is not displayed because of the semi-colon.
Interrupting a Program
A MATLAB program that is caught in a loop can be interrupted
by pressing Ctrl C
=======================================================================================
MATLAB's Interaction With the Computer
Memory
MATLAB interacts directly with the computer memory. During any MATLAB session any variable you use is stored in memory, and can be recalled and used until you clear the memory.
Note: Function files work differently. See the tutorial "Function m-files" for a description of how function files interact with the memory.
>> x = 1
x =
1
>> who % This command lists all variables in memory. It doesn't give the values.
Your variables are:
x
>> clear % This clears the variables in memory
>> who
(Nothing will be displayed. The memory is cleared.)
Be careful, there is no getting them back without re-executing the commands that created them.
Every time a variable name is assigned either directly by you or through a calculation it is stored in memory. The value is not changed unless it is reassigned through a calculation.
>> a = 4;
>> b = 6;
>> c = 2;
>> f = a + b + c
f =
12
>> a =6;
>> f
f =
12
Note that 'f' is not recalculated just because the value of 'a' is changed. To recalculate f with the new value of 'a', use the up arrow key until (f = a + b + c) then press enter.
Variables can be deleted, either individually or altogether.
>> clear a;
>> clear % This clears all variables AND THEY CAN'T BE RETRIEVED
=======================================================================================
Help can be accessed in three different ways:
Saving and Retrieving Data - Section 3.2
Store all your files on a 3.5" diskette. This is the A drive on the computer. If you don't tell it to use the A drive it will default to the harddrive in the directory C:\TEMP. This is not wise. We cannot promise security of your files. Also the temp directory or even the entire C drive will frequently be wiped clean.
You can store all current workspace variables for later use in two ways: This only saves the variable values, not the commands used to create them.
To retrieve a previously stored workspace type LOAD xxx.mat at the MATLAB prompt or click on FILE / LOAD WORKSPACE.
The command >> what, displays the m-files (.m) and workspace files (.mat) available in the current directory.
If you want to see the list of all files stored type DIR at the MATLAB prompt.
=======================================================================================
Number Display Formats - Section 3.3
MATLAB allows you to use a number of different numeric displays on the screen. Using any of these does not change the precision of the number as it is stored in memory.
To change the line spacing on the screen, click on FILE
/ PREFERENCES / GENERAL / FORMAT COMPACT. This will change the line
spacing from double to single spacing, allowing you to see more on the
screen. An alternate method is to type "format compact" at the MATLAB prompt.
The default for MATLAB is to display 4 digits to the right of the decimal point. Listed on page 45 of the MATLAB manual are the different numeric formats. There are two ways to change the numeric display format.
>> format long %
gives 16 digits
>> c = 35/3
c =
11.66666666666666
>> format short e
% 5 digits plus an exponent
>> c
c =
1.1667e+01
>> format long e
% 16 digits plus an exponent
>> c
c =
1.166666666666666e+01
>> format short
% This is the default
>> c
c =
11.6667
=======================================================================================
There are a number of rules as to the variable names you can use.
Special variables can be redefined by you. It's better not to do this.
=======================================================================================
Text after a % sign is taken as a comment statement. This is very useful to in clarify a workspace that will be saved for later use, or for submission. This can be on a separate line or the same line as an executable command, It is the best way to specify the units on a number.
>> x = 4 % x
= the distance from here to there
x =
4
=======================================================================================
Simple Math Operations - Section 1.1
As with most programming languages the numbers or variables with assigned values on the right side of the equal sign are manipulated and the result is assigned to the variable name on the left.
>> b = 5 + 8
b =
13
>> 5 + 8 % If
no output variable is defined the default is "ans"
ans =
13
The known information and operation must be on the right side of the equal sign. MATLAB cannot make sense of the following.
>> 5 + 8 = b
??? 5 + 8 =
Missing operator, comma or semicolon
>> b = b + 5
% A variable can be reassigned.
b =
18
The spaces in mathematical expressions make no difference
Calculations are performed left to right with the following order of preference, starting with the inner parenthesis and working outward.
powers ( symbol ^ )
multiplication ( symbol * ) and division ( symbol / or
\ )
addition ( symbol + ) and subtraction (symbol - )
>> c = 20/5
c =
4
>> c = 20\5
c =
0.2500
>> c = 3*(4+2)
c =
18
>> c = 3 * 4 + 2
c =
14
>> c = 3 ^ (2+1)
c =
27
>> 2^3-4-8/4*2
<<------
ans =
0
>> 2^3-4-8/(4*2)
ans =
3
You may want to add parentheses or spaces for readability, even when they are not needed. For example the command indicated by the arrow above is more readable when written as;
>> (2^3) - 4 - ((8/4) * 2)
ans =
0
=======================================================================================
Scientific Features, Common Mathematical Functions - Section 2.1
MATLAB contains a large number of mathematical features.
These include logarithms, trig functions, and a number of methods for rounding.
A complete listing is in the table on page 15 of the MATLAB manual. Section
1.5 gives a description of complex numbers.
=======================================================================================
Text as a Variable - Section 9.1
There are times when manipulating text can be useful. This can be difficult, and therefore it is typically better to use the 'disp' or 'fprintf' command.
Character strings are handled like row vectors. It is simply text surrounded by single quotes.
>> t = 'the units are gallons per minute'
t =
the units are gallons per minute
You can access a section of the string.
>> u = t(5:13)
% (This displays characters 5 through 13 of the array
t)
u =
units are
Each character, including spaces, is treated as an element of the row vector, t.