Page 120 - A Guide to MATLAB for Beginners and Experienced Users
P. 120

Chapter 7



                     MATLAB Programming















                     Every time you create an M-file, you are writing a computer program using
                     the MATLAB programming language. You can do quite a lot in MATLAB
                     using no more than the most basic programming techniques that we have
                     already introduced. In particular, we discussed simple loops (using for) and
                     a rudimentary approach to debugging in Chapter 3. In this chapter, we will
                     cover some further programming commands and techniques that are useful
                     for attacking more complicated problems withMATLAB. If you are already
                     familiar withanother programming language, muchof this material will be
                     quite easy for you to pick up!


                     Many MATLAB commands are themselves M-files, which you can examine
                       using type or edit (for example, enter type isprime to see the M-file for
                       the command isprime). You can learn a lot about MATLAB programming
                       techniques by inspecting the built-in M-files.




           Branching



                     For many user-defined functions, you can use a function M-file that executes
                     the same sequence of commands for each input. However, one often wants a
                     function to perform a different sequence of commands in different cases, de-
                     pending on the input. You can accomplish this with a branching command, and
                     as in many other programming languages, branching in MATLAB is usually
                     done withthe command if, which we will discuss now. Later we will describe
                     the other main branching command, switch.



                                                                                  101
   115   116   117   118   119   120   121   122   123   124   125