20. Matlab Programming 2

20. Matlab Programming 2 - ENGR 101 Section 100 MATLAB...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
ENGR 101 Section 100 M. Wellman 1 MATLAB Programming (Part II) ENGR 101, Lecture 20: 24 Nov 10 Announcements Project 7 due Wed 1 Dec Try to get done early, to avoid MATLAB license contention
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ENGR 101 Section 100 M. Wellman 2 Relational Operators on Arrays Relational operators apply elt-by-elt between arrays 1 5 > 3 1 0 1 3 -1 -1 2 1 0 They also apply elt-by-elt between a scalar and an array 5 > 8 2 0 1 10 -1 0 1 Selection on Logic Vectors Q: what does the following do, if x is an array (and therefore condition expression is an array of bools)? if x > 20 disp('condition is true') else disp('condition is false') end A: then clause iff condition holds for all elts of x (i.e., condition expr true for all elts), otherwise else clause
Background image of page 2
ENGR 101 Section 100 M. Wellman 3 Example: Vector Equality suppose x and y are vectors of the same length if x == y disp('vectors are equal') else disp('vectors not equal') if not(x == y) disp('vectors not equal') else disp('vectors are equal') if not(all(x == y)) disp('vectors not equal') else disp('vectors are equal') this works as intended! this does not work as intended (why not?) this works as intended! Defining MATLAB Functions MATLAB supports procedural abstraction Define functions within .m files Function definition format: function [ outputs ] = function_name ( inputs ) % H1 line : function name and short description % comments documenting function behavior % Body outputs: comma-separated list of output variable names inputs: comma-separated list of argument variable names
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
M. Wellman 4 .p Files MATLAB stores function definitions in an internal format called pcode avoids repeating some interpreter processing (e.g., parsing text) every time function is invoked a form of compilation, but not to native machine language Can save pcode to a dedicated file with command: pcode fn_name creates file fn_name .p binary format, not readable in text editor Subfunctions A function (.m) file may contain multiple function definitions The first is the primary function and should correspond to the name of the file Subsequent functions are called subfunctions Primary and subfunctions may call each other regardless of order of appearance in file
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/25/2011 for the course ENGR 101 taught by Professor Ringenberg during the Fall '07 term at University of Michigan.

Page1 / 14

20. Matlab Programming 2 - ENGR 101 Section 100 MATLAB...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online