You are on page 1of 32

January 24, 2005 Lecture 3 - By P.

Lin 1
CPET 190
Lecture 3
Problem Solving with MATLAB


http://www.etcs.ipfw.edu/~lin
January 24, 2005 Lecture 3 - By P. Lin 2
Lecture 3: MATLAB
Development Environment
3-1 Desktop Tools
Starting and Quitting MATLAB
Command Window
Running Functions
Entering Variables
Controlling Input/Output
Searching Items
Running Programs
Preferences for the Command
Window
January 24, 2005 Lecture 3 - By P. Lin 3
Lecture 3: MATLAB
Development Environment (continue)
3-1 Desktop Tools (continue)
Command History
The MATLAB Workspace
Help Browser
Workspace Browser
Current Directory Browser
Edit/Debug
January 24, 2005 Lecture 3 - By P. Lin 4
Lecture 3: MATLAB
Development Environment (continue)
3-2 Useful Commands
The MATLAB Search Path
Number Display Formats
Session Log
MATLAB Managing Commands and Functions
demo - Run demos
help - Online help
ver - Current MATLAB and toolbox versions
version - Current MATLAB version number
path - MATLAB Search path
diary - Save typed commands on a named file
Summary
January 24, 2005 Lecture 3 - By P. Lin 5
Starting and Quitting MATLAB
Starting MATLAB
From command window: start -> Run -> MATLAB
Start -> All Programs -> MATLAB
January 24, 2005 Lecture 3 - By P. Lin 6
Starting and Quitting MATLAB
Quitting MATLAB
At the MATLAB
command window:
>> quit
>> exit
Close MATLAB
Window
January 24, 2005 Lecture 3 - By P. Lin 7
Desktop Tools
Started MATLAB and see the three sub-windows:
Command Window
Current Directory
Command History
At the command
window, we enter
>>help help
January 24, 2005 Lecture 3 - By P. Lin 8
Command Window
Running Functions
Entering Variables
Controlling Input/Output
Searching Items
Running Programs
Setting Preferences for the Command
Window
January 24, 2005 Lecture 3 - By P. Lin 9
Running Commands
An Example:
>> magic(3)
ans =
8 1 6
3 5 7
4 9 2

>> help magic

MAGIC Magic square.
MAGIC(N) is an N-by-N
matrix constructed from the
integers
1 through N^2 with equal
row, column, and diagonal
sums.
Produces valid magic
squares for all N > 0 except
N = 2.
15
15
15
15
15
15 15 15
January 24, 2005 Lecture 3 - By P. Lin 10
Entering Variables
>> x = 10
x = 10
>> y = 10
y = 10
>> x * y
ans = 100
>> x / y
ans = 1
>> x + y
ans = 20
>> x - y
ans = 0
>>
January 24, 2005 Lecture 3 - By P. Lin 11
Controlling Input/Output
Format Function
control the
display formats
for numeric
values
Examples
>> x = 1/3
x = 0.3333
>> y = 1.2345e-7
y = 1.2345e-7
>> format short e
>> x
x = 3.3333e-001
>> y
y = 1.2345e-007
Hit Enter key
MATLAB Displays
Hit Enter key
MATLAB Displays
Hit Enter key
Enter x
Enter y
MATLAB Echoes
January 24, 2005 Lecture 3 - By P. Lin 12
Controlling Input/Output (continue)
Format Function:
>> help format

FORMAT Set output format.
All computations in MATLAB are done in
double precision.
FORMAT may be used to switch between
different output
display formats as follows:
FORMAT Default. Same as SHORT.
FORMAT SHORT Scaled fixed point
format with 5 digits.
FORMAT LONG Scaled fixed point
format with 15 digits.
FORMAT SHORT E Floating point
format with 5 digits.
FORMAT LONG E Floating point format
with 15 digits.
>> format long
>> x
x =
0.33333333333333
>> y
y =
1.234500000000000e-
007
January 24, 2005 Lecture 3 - By P. Lin 13
Controlling Input/Output (continue)
Format Function:
>> help format

FORMAT SHORT G Best of fixed or
floating point format with 5 digits.
FORMAT LONG G Best of fixed or
floating point format with 15 digits.
FORMAT HEX Hexadecimal format.
FORMAT + The symbols +, - and
blank are printed for positive, negative
and zero elements. Imaginary parts are
ignored.
FORMAT BANK Fixed format for
dollars and cents.
FORMAT RAT Approximation by ratio
of small integers.
>> format long g
>> x
x =
0.333333333333333
>> y
y = 1.2345e-
007
>> format bank
>> x
x = 0.33
>> y
y = 0.00
>> format rat
>> x
x = 1/3
>> y
y = 1/8100446
January 24, 2005 Lecture 3 - By P. Lin 14
Controlling Input/Output (continue)


File -> Preferences

Allows you to set
up various
numeric display
formats through
GUI
January 24, 2005 Lecture 3 - By P. Lin 15
Suppress Output
Suppressing Output
End of the statement with a semicolon
Increase statement and program execution
speed
An Example
>> x = 10;
>> y = 10;
>> product = x * y
Product = 100
January 24, 2005 Lecture 3 - By P. Lin 16
Entering long Statements
If a statement does not fit on a line (too
long), uses an ellipsis (three periods),
follow by Enter or Return, to indicate the
continuation of the statement to the next
line
For an example
>> x = 1 + 2 + 3 + 5 + 6 + 7 + ...
8 + 9 + 10
x =
51
January 24, 2005 Lecture 3 - By P. Lin 17
Command Line Editing and
Command History
Command Line Editing
Keys such as right arrow , down arrow ,
up arrow , left arrow , etc, can be used
to edit commands
Command History
The entered commands at the Command
Windows are recorded or logged in the
Command History
We can view previous commands or statements
We can copy or execute rerun previous
statements
January 24, 2005 Lecture 3 - By P. Lin 18
The MATLAB Workspace
who function
whos function
clear function
>> who
Your variables are:
x y

>> whos
Name Size Bytes Class
x 1x1 8 double array
y 1x1 8 double array

Grand total is 2 elements using 16 bytes
>> clear x
>> who
Your variables are:
y

>> clear
>> whos
January 24, 2005 Lecture 3 - By P. Lin 19
The MATLAB Workspace
save function
load function
>> save 8_20_2004.mat
>> clear
>> whos
>> load 8_20_2004.mat
>> whos
Name Size Bytes Class

ans 1x1 8 double array
x 1x1 8 double array
y 1x1 8 double array

Grand total is 3 elements using 24 bytes

>> clear
>> whos
Current Directory
January 24, 2005 Lecture 3 - By P. Lin 20
Save Command
We can use help save to see documentation of
SAVE function and learn how to Save workspace
variables to disk.
SAVE FILENAME saves all workspace
variables to the binary "MAT-file, If
FILENAME has no extension, .mat is assumed.
SAVE, by itself, creates the binary "MAT-file"
named 'matlab.mat'. It is an error if
'matlab.mat' is not writable.
SAVE FILENAME X saves only X.
SAVE FILENAME X Y Z saves X, Y, and Z. The
wildcard '*' can be used to save only those
variables that match a pattern.
January 24, 2005 Lecture 3 - By P. Lin 21
Save Command (continue)
MATLAB also offers various saving options:
ASCII Options:
SAVE ... -ASCII uses 8-digit ASCII form
instead of binary regardless of file extension.
SAVE ... -ASCII -DOUBLE uses 16-digit ASCII
form.
SAVE ... -ASCII -TABS delimits with tabs.
And more will be discussed later
January 24, 2005 Lecture 3 - By P. Lin 22
Help Browser
January 24, 2005 Lecture 3 - By P. Lin 23
Workspace Browser
January 24, 2005 Lecture 3 - By P. Lin 24
Current Directory Browser
January 24, 2005 Lecture 3 - By P. Lin 25
The Edit/Debug
Use the Editor/Debugger to create and debug M-files,
which are programs you write to run MATLAB functions.
The Editor/Debugger provides a graphical user interface
for basic text editing, as well as for M-file debugging.
January 24, 2005 Lecture 3 - By P. Lin 26
The Edit/Debug (continue)
January 24, 2005 Lecture 3 - By P. Lin 27
The Edit/Debug (continue)
Create a mlab_exs directory
Save the M-file as sine60hz.m
January 24, 2005 Lecture 3 - By P. Lin 28
The Edit/Debug (continue)
January 24, 2005 Lecture 3 - By P. Lin 29
Testing/Debugging sin60hz.m
Click -> Debug -> Run
January 24, 2005 Lecture 3 - By P. Lin 30
Other Commands
Other Useful MATLAB Management
Commands/Functions
demo - Run demos
help - Online help
ver - Current MATLAB and toolbox
versions
version - Current MATLAB version
number
path - MATLAB Search path
diary - Save typed commands on a
named file
January 24, 2005 Lecture 3 - By P. Lin 31
Summary
Desktop Tools
Starting and Quitting MATLAB
Command Window
Running Functions
Entering Variables
Controlling Input/Output
Searching Items
Running Programs
Preferences for the Command Window
Command History
The MATLAB Workspace
Help Browser
Workspace Browser
Current Directory Browser
Edit/Debug - a sine wave program
January 24, 2005 Lecture 3 - By P. Lin 32
Question?
Answers

Email: lin@ipfw.edu

You might also like