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

Presenting Your Results      43


                     warning message about a nonexistent file the first time you run the script.)
                     You can also get extraneous output in a diary file if you type CTRL+C to halt a
                     script containing a diary command. If this happens, you should type diary
                     off in the Command Window before running the script again.


           Presenting Graphics

                     As indicated in Chapters 1 and 2, graphics appear in a separate window. You
                     can print the current figure by selecting File : Print... in the graphics window.
                     Alternatively, the command print (without any arguments) causes the figure
                     in the current graphics window to be printed on your default printer. Since
                     you probably don’t want to print the graphics every time you run a script, you
                     should not include a bare print statement in an M-file. Instead, you should
                     use a form of print that sends the output to a file. It is also helpful to give
                     reasonable titles to your figures and to insert pause statements into your
                     script so that viewers have a chance to see the figure before the rest of the
                     script executes. For example,
                       xx = 2*pi*(0:0.02:1);
                       plot(xx, sin(xx))
                       % Put a title on the figure.
                       title(’Figure A: Sine Curve’)
                       pause
                       % Store the graph in the file figureA.eps.
                       print -deps figureA
                     The form of print used in this script does not send anything to the printer.
                     Instead, it causes the current figure to be written to a file in the current

                     working directory called figureA.eps in Encapsulated PostScript format.
                     This file can be printed later on a PostScript printer, or it can be imported into
                     another program that recognizes the EPS format. Type help print to see
                     how to save your graph in a variety of other formats that may be suitable for
                     your particular printer or application.
                       As a final example involving graphics, let’s consider the problem of plotting
                     the functions sin(x), sin(2x), and sin(3x) on the same set of axes. This is a
                     typical example; we often want to plot several similar curves whose equations
                     depend on a parameter. Here is a script M-file solution to the problem:

                       echo on
                       % Define the x values.
                       x = 2*pi*(0:0.01:1);
   57   58   59   60   61   62   63   64   65   66   67