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

46        Chapter 3: Interacting with MATLAB


                      3. Add comments to your M-file to explain the meaning of the intermediate
                        calculations you do and to interpret the results.
                      4. If desired, insert the delete and diary statements into the M-file as
                        described above.
                      5. If you are generating graphs, add print statements that will save the
                        graphs to files. Use pause statements as appropriate.
                      6. If needed, run the M-file one more time to produce the final output. Send
                        the diary file and any graphics files to the printer or incorporate them into
                        a document.
                      7. If you import your diary file into a word processing program, you can
                        insert the graphics right after the commands that generated them. You
                        can also change the fonts of text comments and input to make it easier
                        to distinguish comments, input, and output. This sort of polishing is done
                        automatically by the M-book interface; see Chapter 6.


           Fine-Tuning Your M-Files


                     You can edit your M-file repeatedly until it produces the desired output. Gene-
                     rally, you will run the script each time you edit the file. If the program is long
                     or involves complicated calculations or graphics, it could take a while each
                     time. Then you need a strategy for debugging. Our experience indicates that
                     there is no best paradigm for debugging M-files — what you do depends on
                     the content of your file.
                      We will discuss features of the Editor/Debugger and MATLABdebugging
                       commands in the section Debugging in Chapter 7 and in the section
                       Debugging Techniques in Chapter 11. For the moment, here are some general
                       tips.
                          Include clear all and close all at the beginning of the M-file.
                          Use echo on early in your M-file so that you can see “cause” as well as
                          “effect”.
                          If you are producing graphics, use hold on and hold off carefully.
                          In general, you should put a pause statement after each hold off.
                          Otherwise, the next graphics command will obliterate the current one,
                          and you won’t see it.
                          Do not include bare print statements in your M-files. Instead, print to
                          a file.
                          Make liberal use of pause.
   60   61   62   63   64   65   66   67   68   69   70