22
Programming in MATLAB Week 14 – 4/28/09 Kate Musgrave [email protected] .edu

Programming in MATLAB

Embed Size (px)

DESCRIPTION

Programming in MATLAB. Week 14 – 4/28/09. Kate Musgrave [email protected]. 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 - PowerPoint PPT Presentation

Citation preview

Page 1: Programming in MATLAB

Programming in MATLAB

Week 14 – 4/28/09

Kate Musgrave

[email protected]

Page 2: Programming in MATLAB

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

Page 3: Programming in MATLAB

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

Page 4: Programming in MATLAB

A brief discussion of matrices

Page 5: Programming in MATLAB

A brief discussion of matrices

This is one of the many demos available in MATLAB, which demonstrates basic matrix manipulation.

Page 6: Programming in MATLAB

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

Page 7: Programming in MATLAB

.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

Page 8: Programming in MATLAB

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

Page 9: Programming in MATLAB

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

Page 10: Programming in MATLAB

Function

• Functions are more complex than scripts

• Functions have their own local variables

• Functions return output as specified, and can accept input as specified

Page 11: Programming in MATLAB

Function

• Anatomy of a function:

function name (must match file name)

input: xoutput: mean stdev

First line is function declaration

local variables:

x, n, mean, stdev

Examples: stat.m, stat2.m, triDiagMatrix.m, calcERadius.m

Page 12: Programming in MATLAB

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

Page 13: Programming in MATLAB

Flow control

• Conditional control – if, else, switch

• Loop control – for, while, continue, break

• Program termination – return

Page 14: Programming in MATLAB

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

Page 15: Programming in MATLAB

Conditional control – switch

switch variable or statement

case value

statements

case value

statements

otherwise

statements

end

Note: the switch statement does not ‘fall through’, only the true case statement will execute.

Page 16: Programming in MATLAB

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

Page 17: Programming in MATLAB

.mat files• It is convenient to save

your workspace before experimenting with altering variables

• example: our_vars.mat• save filename• load filename

Hint: right-click on heading area to choose information to display

Reminder:

clear – clears workspace

clc – clears command window

Page 18: Programming in MATLAB

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

Page 19: Programming in MATLAB

MATLAB File I/O

List of file types that MATLAB can read in and write out (version 2009a).

Third-party software is available for netCDF files in older versions of MATLAB

You can import any of these file formats (except XLSB, XLSM, HDF5, and platform-specific video) using the Import Wizard or the importdata function.

Page 20: Programming in MATLAB

MATLAB File I/O

Article on importing SDF files (includes importing into MATLAB): http://findarticles.com/p/articles/mi_m0HPJ/is_n6_v44/ai_14823379/pg_4/

Notes on other formats:

Page 21: Programming in MATLAB

File I/O Example

• readField.m

Page 22: Programming in MATLAB

Questions?