Docsity

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

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 programmingLanguage – Strengths (as

compared to C/C++/Fortran)

Fast to write -no type declarations needed - Memory allocation/deallocation handledautomatically - Functions and scripts can be accessedautomatically from path - Vector/Matrix operations in many dimensionsbuilt in - Polymorphic with respect to matrix dimensions

Matlab as a programming

Language - Weaknesses

  • Expensive! • Debugging complex code can be tricky • Not object-oriented • Behavior of built in functions must be learned. - No type declarations needed (example) • Namespace collisions (example)

Outline

Scripts vs. Functions

Techniques for writing functions - Structures - Cell-Arrays - Examples

Matlab Scripts

plotsquared.m: x = [-5:5]; y = x .^2; plot(x,y) Run with ‘plotsquared’

Advantages of Scripts

  • Easy to write • Easy to patch together and nest • Can be cut and paste directly into Matlab command line - Can use any variables already in the workspace

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)

Matlab Functions

Example (complex_phase_and_magnitude.m) - Arguments are ‘passed by value’. - All variables are internal (unless you useGLOBAL variables), so your workspace is notaltered. - Can be called with fewer than specified inputarguments. - Can be called with fewer than specified outputarguments. - Functions can have indefinite arguments at theend of the argument list

Advantages of Functions

Reuseable – develop your own libraries, or getfunctions from others - Robust – rules all in one place (debug sharedmethods once) - Sharable - Self-documenting - Can be used easily customize built-in matlabfunctions - Provide variable scope safety - Can be called inside of scripts - ‘one rule, one place’

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, notjust scalars (avoid looping over matrices). - If you see repeated similar procedures, consider loopingover 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)

Matlab Structures

  • Group variables • Group data of different sizes/types (meta- data with variables) - Sort by name • Nest

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