1 / 22

Programming in MATLAB

Programming in MATLAB. Week 14 – 4/28/09. Kate Musgrave kate@atmos.colostate.edu. 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

Download Presentation

Programming in MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming in MATLAB Week 14 – 4/28/09 Kate Musgrave kate@atmos.colostate.edu

  2. 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 Syllabus

  3. 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

  4. A brief discussion of matrices

  5. A brief discussion of matrices This is one of the many demos available in MATLAB, which demonstrates basic matrix manipulation.

  6. 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

  7. .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

  8. 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

  9. 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

  10. Function • Functions are more complex than scripts • Functions have their own local variables • Functions return output as specified, and can accept input as specified

  11. 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 input: x Examples: stat.m, stat2.m, triDiagMatrix.m, calcERadius.m

  12. 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

  13. Flow control • Conditional control – if, else, switch • Loop control – for, while, continue, break • Program termination – return

  14. 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

  15. 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.

  16. 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

  17. .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

  18. 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

  19. 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 platform-specific video) using the Import Wizard or the importdata function. Third-party software is available for netCDF files in older versions of MATLAB

  20. 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/

  21. File I/O Example • readField.m

  22. Questions?

More Related