Professional Documents
Culture Documents
2 of 58
What is MATLAB?
Mathematical computations
Algorithmic development
3 of 58
4 of 58
My Remarks (subjective)
Spreadsheets (Excel)
Easy to use
Good for general purpose computation
Nice standard graphics
Good connectivity to other applications
Platform independent
5 of 58
6 of 58
Tutorial Outline
Input-output commands
7 of 58
8 of 58
MATLAB Foundations
MATLAB has advanced data structures including objectoriented programming functionality and overloadable
operators
Virginia Polytechnic Institute and State University
9 of 58
10 of 58
Command Window
- to execute commands in the MATLAB environment
Figure Window
- to display graphical output from MATLAB code
11 of 58
Workspace Window
- to view variable definitions and variable memory
allocations
12 of 58
Workspace
Window
Current
Folder
Command
Window
Command
History
Virginia Polytechnic Institute and State University
13 of 58
14 of 58
15 of 58
16 of 58
17 of 58
MATLAB Editor/Debugger
MATLAB has an interactive debugger to help you step
through your source code. This debugger has many of the
same functional features found in high-level
programming languages (i.e., FORTRAN, C/C++, etc.).
18 of 58
MATLAB Debugger
Breakpoints
Break on error, warnings and overflows
Step in and out of script
Function dependencies
19 of 58
20 of 58
PlotEdit
- allows interactive changes to plots (add legend, lines,
arrows, etc.)
- This function is automatically invoked in MATLAB 5.3
PropEdit
- Allows changes to all Handle Graphic properties in a
MATLAB plot
- Requires knowledge of Handle Graphics (more on this
later)
21 of 58
22 of 58
MATLAB Workspace
As you develop and execute models in MATLAB the
workspace stores all variables names and definitions for
you. All variables are usually available to you unless the
workspace is clear with the >>clear command.
23 of 58
24 of 58
Provides acces to various help files (both internal and online files available on the web)
25 of 58
Computer RAM
26 of 58
Computer RAM
Swap Space Memory
MATLAB
Application
Another
Application
27 of 58
28 of 58
29 of 58
Remarks
Command line
Interactive mode
Good for quick computations or
changes
Semi-interactive mode
Good to prototype small to complex models
Used most of the time
Platform independent
30 of 58
Multiple commands can be executed using the semicolon ; separator between commands
>> a=[3 2 4; 4 5 6; 1 2 3] ; b=[3 2 5] ; c=a*b
This single line defines two matrices (a and b) and
computes their product (c)
31 of 58
32 of 58
demo
who
whos
clear
33 of 58
load
save
quit
what
edit
diary
34 of 58
changes directory
copyfile
copy a file
dir
pwd
delete
delete a file
mkdir
make a directory
dos
unix
35 of 58
36 of 58
MATLAB M-Files
37 of 58
Sample M-File
The following file generates random numbers
% Sample file to generate Random Numbers using
% MATLAB built-in functions
ntrials = 1000;
i=1:1:ntrials;
RU(i) = rand(1,ntrials);
38 of 58
39 of 58
Output of randem.m
The following figure illustrates the output of randem.m
140
120
No. of Trials
100
80
60
TextEnd
40
20
0
0.1
0.2
0.3
0.4
0.5
RN
0.6
0.7
0.8
0.9
40 of 58
41 of 58
plot(x,y)
- where:
- x is the independent variable (index i in our case)
- y is the dependent variable (values of RU and RN)
42 of 58
RN and RU
0
TextEnd
-1
-2
-3
-4
100
200
300
400
500
Index i
600
700
800
900
1000
43 of 58
44 of 58
Fast retrieval
45 of 58
46 of 58
47 of 58
48 of 58
49 of 58
50 of 58
51 of 58
Import
Command
52 of 58
53 of 58
54 of 58
55 of 58
= y(: , 1);
= y(: , 2);
= y(: , 3);
= y(: , 4);
56 of 58
fclose(fid);
56a
Header
Data
56b
8369
56c
fclose(fid);
8369
56d
56e
>> readData
readData = {23x1 cell}
{23x1 cell}
Bridge Name
Country
Year
Completed
Length (m)
Virginia Tech (copyright A.A. Trani)
56f
56g
readData{1}(3,1)
references the third row
element of the cell array
56h
readData{1}(3:5,1)
references the third, fourth
and fifth row elements of
the cell array
56i
56j
readData{3}(1:5,1)
references the first five row
elements of the third
column of the cell array
56k
56l
Bridges_of_the_world_short.xls
Virginia Tech (copyright A.A. Trani)
56m
56n
Observations
num is a standard
numeric array as
shown
Elements of num
can be referenced
in the usual
(row,column)
format
num(2,2)=8230
56o
Observations (2)
Elements of txt
can be referenced
using the cell array
nomenclature
cell{i}(row,column)
txt{1,2}=Country
56p
readData{3}(1:5,1)
txt{1}(1,2)=N
56q
56r
57
>> save
save
fname
function file name
list of
variables
57a
binary
file saved
list of
variables saved
Virginia Tech (copyright A.A. Trani)
57b
dlmwrite('FILENAME',M,'DLM')
57c
57d
57e
fprintf (fid,format,A)
57f
57g
57h
More Explanations
57i
myOutput.txt
ASCII text
file produced
57j
57k
57l
xlswrite('exportToExcel.xls',num)
Writes to Excel starting at cell A1
57m
57n
Matlab Introduction
Handling Basic Searches in Matrices and
Arrays
Dr. Antonio A. Trani
Professor
Dept. of Civil and Environmental Engineering
58
58a
58b
Import Procedure
Saving as column vectors
We select to import
using column vectors
This way each column is
saved as a separate
variable facilitating its
use later on.
After importing
58c
Four variables
were created:
1) Name
2) Country
3) Completedyear
4) Length
58d
58e
58f
58g
58h
58i
58j
58k
indices of variable
Country that meet
the criteria
1 = true
0 - false
Virginia Tech (copyright A.A. Trani)
58l
Value of indices3
are:
1 = true
0 - false
Virginia Tech (copyright A.A. Trani)
58m
58n
Summary
58o