Introduction to MATLAB - Department of Atmospheric Science

Download Report

Transcript Introduction to MATLAB - Department of Atmospheric Science

Programming in MATLAB
Week 14 – 4/28/09
Kate Musgrave
[email protected]
Syllabus
• Week 13: T 4/21
–
–
–
–
Intro to MATLAB
MATLAB GUI
Variables
Operations
• Week 14: T 4/28
–
–
–
–
–
Functions and scripts
Programming style
Comments
Flow control
File I/O
• Week 15: T 5/5
–
–
–
–
–
Graphics
Plot types
Figure window
Figure properties
Figures: special topics
• Week 16: T 5/12
–
–
–
–
MATLAB toolboxes
Statistics
Signal processing
Special topics
A brief discussion of matrices
• As mentioned previously, MATLAB is
optimized for matrices
• MATLAB regards all variables as matrices
– Scalars are 1x1 matrices
– Vectors are 1xn or nx1 matrices
– 2D matrices are the default, more than 2D are
considered multidimensional arrays
(nxmxlx…)
• The class of the matrix is determined by
the data type stored within
A brief discussion of matrices
A brief discussion of matrices
This is one of the many demos
available in MATLAB, which
demonstrates basic matrix
manipulation.
Files used in MATLAB
• .m files
– Both functions and scripts are stored in .m files
• .mat files
– The MATLAB workspace (or specific variables)
can be saved in .mat files
– These files are easy to save and load, and
MATLAB accessing them is very efficient
• .fig files (next week)
– Plots can be saved in .fig files, and then the
figure can be edited without reloading data
.m files
• Code can be saved in .m files and run in
the command window – exact
implementation depends on whether the
code is a function or a script
Script
• Simplest kind of m-file
• Type up a bunch of commands and save as
filename.m
• Type filename in command window to run
• Example: first_program.m
Script
• Scripts have access to the variables in the
workspace, both to read and to write
– Changes done to variables in a script will
remain after the script is finished
• Scripts are useful for the automation of
repetitive tasks
Function
• Functions are more complex than scripts
• Functions have their own local variables
• Functions return output as specified, and
can accept input as specified
Function
• Anatomy of a function:
function name (must match file name)
First line is function declaration
local variables:
x, n, mean, stdev
output: mean stdev
Examples: stat.m, stat2.m, triDiagMatrix.m, calcERadius.m
input: x
Commenting
• Comment your code!
• Any line starting with % is a comment
• Comments can be added to the end of
existing lines by adding a %
– Note that anything after % will be ignored
• In editor screen comments are green
• Any comments written at the beginning of
an m-file will be displayed by the
command help filename
Flow control
• Conditional control – if, else, switch
• Loop control – for, while, continue, break
• Program termination – return
Conditional control – if, else, elseif
if test statement
statements
elseif test statement
statements
else
statements
end
Note that ==,~=,>,<
are all scalar tests.
To test matrices, try:
isequal
isempty
all
any
Conditional control – switch
switch variable or statement
case value
statements
case value
statements
Note: the switch statement does
not ‘fall through’, only the true
otherwise
case statement will execute.
statements
end
Loop control – for, while
for varname = min:max
statements
end
while condition is true
statements
end
Note: continue, break and return
-continue skips the remainder of
the loop to pass control to the
next iteration
-break exits from the loop early
-similar to break, return exits the
program (script or function) early
.mat files
• It is convenient to save
your workspace before
experimenting with altering
variables
• example: our_vars.mat
• save filename
• load filename
Reminder:
clear – clears workspace
clc – clears command window
Hint: right-click on heading area to choose information to display
File Input/Output
• Several methods exist in MATLAB
depending on the type of file you are trying
to read or write
• Easiest method – the import data wizard
– File  Import Data…
– Follow on-screen instructions
• In scripts, use importdata command
– help importdata
MATLAB
File I/O
List of file types that
MATLAB can read in and
write out (version 2009a).
You can import any of these
file formats (except XLSB,
XLSM, HDF5, and platformspecific video) using the
Import Wizard or the
importdata function.
Third-party software is
available for netCDF files in
older versions of MATLAB
MATLAB File I/O
Notes on other formats:
Article on importing SDF files (includes importing into MATLAB):
http://findarticles.com/p/articles/mi_m0HPJ/is_n6_v44/ai_14823379/pg_4/
File I/O Example
• readField.m
Questions?