Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Matlab Advanced Programming: Strengths, Weaknesses, Scripts vs. Functions, and Guidelines , Study notes of Electrical and Electronics Engineering

An overview of matlab as a programming language, focusing on its strengths and weaknesses compared to other languages like c/c++/fortran. It also covers the use of scripts and functions, techniques for writing functions, and guidelines for writing effective functions. Examples and references for further learning.

Typology: Study notes

Pre 2010

Uploaded on 07/31/2009

koofers-user-6uw
koofers-user-6uw 🇺🇸

10 documents

1 / 27

Toggle sidebar

Related documents


Partial preview of the text

Download Matlab Advanced Programming: Strengths, Weaknesses, Scripts vs. Functions, and Guidelines and more Study notes Electrical and Electronics Engineering in PDF only on Docsity! Matlab Advanced Programming Matt Wyant University of Washington Matlab as a programming Language – Strengths (as compared to C/C++/Fortran) • Fast to write -no type declarations needed • Memory allocation/deallocation handled automatically • Functions and scripts can be accessed automatically from path • Vector/Matrix operations in many dimensions built in • Polymorphic with respect to matrix dimensions Outline • Scripts vs. Functions • Techniques for writing functions • Structures • Cell-Arrays • Examples Not included • Matlab graphics • Object-oriented programming • Interfaces to other languages • Not much on handling matrices Matlab Scripts plotsquared.m: x = [-5:5]; y = x .^2; plot(x,y) Run with ‘plotsquared’ Ways to protect against scope problems in scripts • clear all – include at top of script or type it – sometimes not desirable • Initialize all variables you use • Beware assignment to parts of an un- initialized array Suppose y is a 20x20 array. Then the following script is run: x = [-5:5]; y(1:11) = x .^2; plot(x,y) Fixed version: y = zeros(1:11) x = [-5:5]; y(1:11) = x .^2; plot(x,y) Disadvantages of Functions • Designing them takes time and practice. • Debugging can be more involved than for scripts. Guidelines for writing functions • Write at least minimal documentation – required inputs – optional inputs – outputs – units • Use a descriptive function name • Use descriptive variable names • Possibly check the input arguments – size – number of dimensions – type Guidelines for writing functions • If you can, don’t assume the size of input matrices • Write functions to apply a process to whole matrices, not just scalars (avoid looping over matrices). • If you see repeated similar procedures, consider looping over them or making them a function. • If you can, don’t assume the size of input matrices • Avoid GLOBAL variables. – Make them arguments instead. – If you need persistent variables use ‘persistent’. – If you need lots of physical constants pass them in as a structure) • Small simple functions can be very useful (examples) Cell-arrays • Containers for multiple Matlab objects of different types and sizes • Useful for grouping character strings • Can be used as comma-separated lists • () used to get subset of cell array • {} used to get contents of cells Examples • Exploiting Matlab matrix handling • Passing back complex output using structures • Extending existing Matlab functions using varargin • Looping over variables with cell-arrays Exploiting Matlab matrix handling • Many built-in and user made functions can handle arbitrarily sized matrices. • Matrices typically must all be the same size. • The functions must use element-by-element math methods on these matrices: x .^ y x .* y x ./ y • Example: (qsat.m), (sat_vapor_pressure.m) Looping over variables with cell- arrays Cell-arrays provide a convenient way to loop through variable data (noloop.m, loop.m). References • Matlab Help – help – help [topic] – help [function name] – doc [function name] • www.mathworks.com • Graphics and GUI’s with MATLAB, Patrick Marchand and O. Thomas Holland • This talk will be placed (soon) at ‘www.atmos.washington.edu/~mwyant Additional Topics • Matrix manipulations and higher dimensional matrices • Handle Graphics Techniques • Matlab Graphics for Publication and Presentation • Performance Tips • NetCDF Interface • Handling Missing Data • Interface with C/C++ or Fortran
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved