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

36        Chapter 3: Interacting with MATLAB


                       y=
                       0.9983     1.0000      1.0000

                       After completing a calculation, you will often realize that the result is not
                     what you intended. The commands above displayed only 5 digits, not 15. To
                     display 15 digits, you need to type the command format long and then
                     repeat the line that defines y. In this case you could simply retype the latter
                     line, but in general retyping is time consuming and error prone, especially for
                     complicated problems. How can you modify a sequence of commands without
                     retyping them?
                       For simple problems, you can take advantage of the command history fea-
                     ture of MATLAB. Use the UP- and DOWN-ARROW keys to scroll through the list
                     of commands that you have used recently. When you locate the correct com-
                     mand line, you can use the LEFT- and RIGHT-ARROW keys to move around in the
                     command line, deleting and inserting changes as necessary, and then press
                     the ENTER key to tell MATLAB to evaluate the modified command. You can
                     also copy and paste previous command lines from the Command Window, or
                     in the MATLAB 6 Desktop from the Command History window as described
                     earlier in this chapter. For more complicated problems, however, it is better to
                     use M-files.


           M-Files


                     For complicated problems, the simple editing tools provided by the Command
                     Window and its history mechanism are insufficient. A much better approach
                     is to create an M-file. There are two different kinds of M-files: script M-files
                     and function M-files. We shall illustrate the use of both types of M-files as we
                     present different solutions to the problem described above.
                       M-files are ordinary text files containing MATLAB commands. You can cre-
                     ate and modify them using any text editor or word processor that is capable of
                     saving files as plain ASCII text. (Suchtext editors include notepad in Win-
                     dows or emacs, textedit, and vi in UNIX.) More conveniently, you can use
                     the built-in Editor/Debugger, which you can start by typing edit, either by
                     itself (to edit a new file) or followed by the name of an existing M-file in the
                     current working directory. You can also use the File menu or the two leftmost
                     buttons on the tool bar to start the Editor/Debugger, either to create a new
                     file or to open an existing file. Double-clicking on an M-file in the Current
                     Directory browser will also open it in the Editor/Debugger.
   50   51   52   53   54   55   56   57   58   59   60