Page 21 -
P. 21

errors will be time-consuming to fix because if you are working in the com-
                             mand window, you need to retype all or part of the program. Even if you
                             do not make any mistakes (!), all of your work may be lost if you inadvert-
                             ently quit MATLAB and have not taken the necessary steps to save the con-
                             tents of the important program that you just finished developing. To
                             preserve large sets of commands, you can store them in a special type of file
                             called an M-file.
                              MATLAB supports two types of M-files: script and function M-files. To hold
                             a large collection of commands, we use a script M-file. The function M-file is
                             discussed in Chapter 3. To make a script M-file, you need to open a file using
                             the built-in MATLAB editor. For both Macs and PCs, first select New from the
                             file menu. Then select the M-file entry from the pull-down menu. After typing
                             the M-file contents, you need to save the file:

                                For Macs and PCs, select the save as command from the file win-
                                   dow. A field will pop up in which you can type in the name you
                                   have chosen for this file (make sure that you do not name a file by
                                   a mathematical abbreviation, the name of a mathematical function,
                                   or a number). Also make sure that the file name has a .m extension
                                   added at the end of its name.
                                For Macs, save the file in a user’s designated volume.
                                For PCs, save the file in the default (bin) subdirectory.

                              To run your script M-file, just type the filename (omitting the .m extension
                             at its end) at the MATLAB prompt.


                             Example 1.7
                             For practice, go to your file edit window to create the following file that you
                             name myfile.m.


                                clear, clf
                                x1=1;y1=.5;x2=2;y2=1.5;x3=3;y3=2;
                                plot(x1,y1,'o',x2,y2,'+',x3,y3,'*')
                                axis([0 4 0 4])
                                xlabel('xaxis')
                                ylabel('yaxis')
                                title('3points in a plane')

                             After creating and saving myfile.m, go to the MATLAB command window
                             and enter myfile. MATLAB will execute the instructions in the order of the
                             statements stored in your myfile.m file.



                             © 2001 by CRC Press LLC
   16   17   18   19   20   21   22   23   24   25   26