1 / 9

Introduction to MATLAB: Understanding Branching Statements and Plotting Techniques

This guide covers the fundamentals of branching statements in MATLAB, including the use of `switch`, `case`, and `otherwise`. It explains how to evaluate expressions and execute corresponding statements based on conditional checks, with examples for clarity. The section further illustrates the concept of plotting multiple graphs in a single window using `subplot`, providing practical examples for sine, cosine, and exponential functions. Resources for further learning are also included, such as recommended reading materials and lecture slides.

dmitri
Télécharger la présentation

Introduction to MATLAB: Understanding Branching Statements and Plotting Techniques

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. CS 170 – Intro to Scientific and engineering Programming

  2. Branching statements • If • Switch

  3. switch, case, otherwise end Let VAL be value of expression. Assume VAL is a scalar double. switch expression case testval1 statements1 case testval2 statements2 otherwise statementsOther end • Does VAL equal testval1? • Yes: Execute statements1 • Jump past end. • No: • Does VAL equal testval2? • Yes: Execute statements2 • Jump past end • No: • Execute statementsOther • Jump past end • Move to code beyond end Any number of cases are allowed. There does not have to be an otherwise (and associated statements).

  4. switch, case, otherwise end VAL (the value of expression) need not be a scalar double. It can also be a char array. Matlab uses strcmp (string compare) to check equality of VAL to the various testval1, testval2, etc. switch expression case testval1 statements1 case testval2 statements2 otherwise statementsOther end

  5. switch, case, otherwise end switch expression case testval1 statements1 case value2 statements2 otherwise statementsOther end testvalcan also be cell arrays.The equality of VAL (value of expression) is tested for any of the contents of the cell array testval.

  6. Switch example if x == 1 fprintf('One'); elseifx == 2 fprintf('Two'); elseifx == 3 fprintf('Three'); else fprintf('%d', x); end switch x case 1 fprintf('One'); case 2 fprintf('Two'); case 3 fprintf('Three'); otherwise fprintf('%d', x); end

  7. More plots • Subplot: putting multiple plots in one window x=0:.1:2*pi;subplot(2,2,1);plot(x,sin(x));subplot(2,2,2);plot(x,cos(x));subplot(2,2,3)plot(x,exp(-x));subplot(2,2,4);plot(peaks);

  8. Questions??

  9. Resources • “Introduction to Programming with Matlab”, J. Michael Fitzpatrick and John D. Crocetti • Lecture slides E77, Andy Packard, http://jagger.me.berkeley.edu/~pack/e77

More Related