2.Interacting With MATLAB This chapter covers the following topics: the Com Writing tiny programs in the Command window &en&pymhsgoinchtirgadofacsmoloms cting errors i ommand 29 s as scripts(.m files) 2.8 Keeping a diary 29 Practicing interacting with MATLAB The commands that are introduced and the sections in which they are premiered are: calendar (2.2) clc (2.2) ctrl-c 22 date disp 98998898 who 2.2 ;(output suppression) up-arrow (2.5) commandwindow ctrl-[ 8 ctrl-] 2.6 ctr1-0 (zero) 2.6 ctrl-i (2.6 22
22 2. Interacting With MATLAB This chapter covers the following topics: 2.1 Using MATLABÊs windows 2.2 Using the Command window 2.3 Writing tiny programs in the Command window 2.4 Allowing or suppressing outputs by omitting or including end-of-line semi-colons 2.5 Correcting errors in the Command window 2.6 Writing, saving, and running larger programs as scripts (.m files) 2.7 Running and debugging MATLAB programs 2.8 Keeping a diary 2.9 Practicing interacting with MATLAB The commands that are introduced and the sections in which they are premiered are: calendar (2.2) clc (2.2) ctrl-c (2.2) date (2.2) disp (2.2) doc (2.2) exit (2.2) help (2.2) ls (2.2) open (2.2) pwd (2.2) quit (2.2) ver (2.2) who (2.2) ; (output suppression) (2.4) up-arrow (2.5) % (2.6) . (2.6) commandwindow (2.6) ctrl-[ (2.6) ctrl-] (2.6) ctrl-0 (zero) (2.6) ctrl-i (2.6)
Interacting With MATLAB 23 2.6) kev 2.6 New Script button 2.6) Run button (2.6 diary (2.8) type (2.8) 2.1 Using MATLAB's Windows To use MATLAB,you must launch the program.MATLAB is activated,as are most com- puter applications,by clicking on its icon on the computer desktop or wherever its icon is located.When MATLAB is running.a number of windows will be opened,often as panes docked together in a single window. When MATLAB is first launched,the Command window appears as a pane in the com- posite window (the one with the name beginning"MATLAB .,”followed by the ver- sion of MATLAB that you are running).The Command window is the most important window in MATLAB.It is where you control what happens and where you see the results of your programming efforts.The Command window will be described in more detail in Section 2.2 The second most i mportant window is the Editor windo ow (the one nam wed by t ion al )Here you MATL ng cap es by wn ng.revisi g.anc ggestions for he arrange these The two windows iust mentioned are the c es that ar used to write and run MATLAB p ams.Ther e are alsoo s.however. which are more specialized and ar described briefly below. One is the Help window.This window provides a portal to MATLAB's tutorials.The Help window can be opened directly by ente ing a command in the search bar at the top right of the MATLAB window,or it can be opened indirectly by typing the doc command in the Command window. The Command History window chronicles the commands used in the Command window You can use this information to remind you what commands you have issued in a MAT LAB session. The Current Folder window lists the contents of the working directory.You will learn how to change the Current Folder in Chapter 6("Input-Output").By default,the Current Folder is set to My Documents/MATLAB in Windows,and Documents/MATLAB in Mac OS
Interacting With MATLAB 23 edit (2.6) F5 key (2.6) New Script button (2.6) Run button (2.6) diary (2.8) type (2.8) 2.1 Using MATLAB’s Windows To use MATLAB, you must launch the program. MATLAB is activated, as are most computer applications, by clicking on its icon on the computer desktop or wherever its icon is located. When MATLAB is running, a number of windows will be opened, often as panes docked together in a single window. When MATLAB is first launched, the Command window appears as a pane in the composite window (the one with the name beginning „MATLAB . . . ,‰ followed by the version of MATLAB that you are running). The Command window is the most important window in MATLAB. It is where you control what happens and where you see the results of your programming efforts. The Command window will be described in more detail in Section 2.2. The second most important window is the Editor window, which usually appears as a separate window (the one named „Editor -. . .‰ followed by the location and name of the file you are editing). Here you exploit MATLABÊs editing capabilities by writing, revising, and saving program scripts and functions, both of which are files that end with a .m suffix. The Editor window will be discussed in Section 2.3. Suggestions for how best to arrange these windows will be given in Section 2.5. The two windows just mentioned are the ones that are most critical. Both are normally used to write and run MATLAB programs. There are also several other windows, however, which are more specialized and are described briefly below. One is the Help window. This window provides a portal to MATLABÊs tutorials. The Help window can be opened directly by entering a command in the search bar at the top right of the MATLAB window, or it can be opened indirectly by typing the doc command in the Command window. The Command History window chronicles the commands used in the Command window. You can use this information to remind you what commands you have issued in a MATLAB session. The Current Folder window lists the contents of the working directory. You will learn how to change the Current Folder in Chapter 6 („Input-Output‰). By default, the Current Folder is set to My Documents/MATLAB in Windows, and Documents/MATLAB in Mac OS
24 Interacting With MATLAB The Workspace window lists the variables that are currently active,giving their names and values.The values of a variable can be viewed in this window in spreadsheet form by clicking on the grid icon to the left of its name. Other windows,called Figure windows,can be created,opened,and closed in your pro- grams to show graphics,text,and other related information (e.g.sounds).Details will be given in Chapter 9("Plots"). 2.2 Using the Command Window As mentioned above,after MATLAB is activated,it brings up the Command window.This is the window where you can issue commands.You do so by typing after the >prompt. Some useful commands that can be typed after given below.followed this nst no because ally”in a way tha ome o nore tha others. requent ones,in our experience,are help,1s.pwd,edit,open, ctrl-c,and exit. ver yo ult with nation. date The current date (in a format you can specify). disp The value of an expression(numeric or string),displayed in the Command window calendar The calendar for the current month. help Topics for which help can be provided within the command window.Adding a topic name after help(followed by a space) brings up help about that topic,provided it is known to MAT LAB.You can find out what topics are known to MATLAB by first typing help alone.This brings up all the categories for which he lp is available. doc This is a shortcut to the Help window,where all the help that can b e viewed in the Command window is available,plus more accessed via the Help tab at the pwd wind he curre dire e listed in the Curren on fo saving a script (pw stands for"print working directory") Lists the contents of the c t part of a file wing a space) vith
24 Interacting With MATLAB The Workspace window lists the variables that are currently active, giving their names and values. The values of a variable can be viewed in this window in spreadsheet form by clicking on the grid icon to the left of its name. Other windows, called Figure windows, can be created, opened, and closed in your programs to show graphics, text, and other related information (e.g., sounds). Details will be given in Chapter 9 („Plots‰). 2.2 Using the Command Window As mentioned above, after MATLAB is activated, it brings up the Command window. This is the window where you can issue commands. You do so by typing after the >> prompt. Some useful commands that can be typed after the >> prompt are given below, followed by the purposes they serve. It will be helpful for you to read through this list now because the commands are listed more or less „chronologically,‰ in a way that corresponds to what occurs in a typical MATLAB session. Some of the commands tend to be used more than others. The most frequent ones, in our experience, are help , ls , pwd , edit , open , ctrl-c , and exit. ver Information about your license, computer, and MATLAB version, together in a convenient summary. If you consult with MathWorks support, you will need this information. date The current date (in a format you can specify). disp The value of an expression (numeric or string), displayed in the Command window. calendar The calendar for the current month. help Topics for which help can be provided within the command window. Adding a topic name after help (followed by a space) brings up help about that topic, provided it is known to MATLAB. You can find out what topics are known to MATLAB by first typing help alone. This brings up all the categories for which help is available. doc This is a shortcut to the Help window, where all the help that can be viewed in the Command window is available, plus more. The Help navigator can also be accessed via the Help tab at the top of the main MATLAB window. pwd Identifies the current directory, the one listed in the Current Folder window, and the default location for saving a script. ( pwd stands for „print working directory‰.) ls Lists the contents of the current directory. Adding just part of a file name after ls (following a space) with an asterisk
Interacting With MATLAB 25 m01 09101m.and ided th nt director is tim pro n lists t im program_02.m andtimmy_progr cam 101.m,but not timothy.doc. open Opens a file in the current directory or invokes other programs as needed (e.g.,Adobe Acrobat for.pdf files). who Lists the names of the currently active variables. whos Lists the names of the currently active variables along with their sizes and other attributes. ctrl-c Holding down the ctrl key and then pressing the c key interrupts the program that is currently running.provided the Command window is the active window(the window in front of any others that are open).This is very useful when you have"runaway" programs and unwanted data are being spewed on the screen or when you have a program that t is running for a long time with out any output that you actually want. clc Clears the Command window exit Terminates MATLAB quit Runs an optional program calledin on ents can AB.just as 2.3 Writing Tiny Programs in the Command Window The of commands isjusta small subset of thosethat can be typed in the Cor In fact the sible an be ndow is infinite.bec ctyped or pate er the coman nds of arbitr ngth and prompt(>>). In practice.typing or pasting very long series of commands is not a good idea,however because the longer and more complex the commands,the greater the chance of error.Once your sequence of commands has grown to a few lines(or is expected to be several lines long).it is better to generate program scripts "off-line"in MATlAB's Editor.There.the scripts can be saved and modified.We will turn to the Editor in the next section.In this section,setting the stage for what will come when we turn to the Editor per se and to acquaint you with some elementary programming.we will consider a few tiny programs that can be written in the Command window.The rules governing acceptable command syntax are the same whether the commands are typed into the command line "by hand
Interacting With MATLAB 25 replacing part of the file name causes all the files with that named part to be listed. Thus, ls tim* lists tim_program_01.m , tim_program_02 . m , timmy_ program_101.m , and timothy.doc, provided these files exist in the current directory. ls tim*.m lists tim_ program_02.m , and timmy_program_101.m , but not timothy.doc . open Opens a file in the current directory or invokes other programs as needed (e.g., Adobe Acrobat for .pdf files). who Lists the names of the currently active variables. whos Lists the names of the currently active variables along with their sizes and other attributes. ctrl-c Holding down the ctrl key and then pressing the c key interrupts the program that is currently running, provided the Command window is the active window (the window in front of any others that are open). This is very useful when you have „runaway‰ programs and unwanted data are being spewed on the screen or when you have a program that is running for a long time without any output that you actually want. clc Clears the Command window. exit Terminates MATLAB. quit Runs an optional program called fi nish.m , whose contents can be customized by the user, then terminates MATLAB, just as exit does. 2.3 Writing Tiny Programs in the Command Window The preceding list of commands is just a small subset of those that can potentially be typed in the Command window. In fact, the number of possible commands that can be typed in the Command window is infinite, because a series of commands of arbitrary length and complexity can be typed or pasted after the command line prompt ( >> ). In practice, typing or pasting very long series of commands is not a good idea, however, because the longer and more complex the commands, the greater the chance of error. Once your sequence of commands has grown to a few lines (or is expected to be several lines long), it is better to generate program scripts „off-line‰ in MATLABÊs Editor. There, the scripts can be saved and modified. We will turn to the Editor in the next section. In this section, setting the stage for what will come when we turn to the Editor per se and to acquaint you with some elementary programming, we will consider a few tiny programs that can be written in the Command window. The rules governing acceptable command syntax are the same whether the commands are typed into the command line „by hand‰
26 Interacting With MATLAB or are part of a file in the Editor.Therefore,typing commands into the Command window can be a good way to experiment with getting the syntax right before you add the lines to an edited program. One of the most fundamental programming tasks is to assign a value to a variable.Suppose you want to assign the number 2 to some variable,arbitrarily called A.This can be done by typing A=2 after the command line prompt as follows: Code2.3.1: 1>>A=2 Output 2.3.1: A= 2 The ordering of terms in the assignment is important,as shown below. Code2.3.2: 1>>2■A Output 2.3.2: ?2?2=A Error:The expression to the left of the equals sign is I not a valid target for an assignment. The error message indicates that.in contrast to mathematics.where an equation means the same thing regardless of whether terms appear to the left or right of the equal sign order matters in MATLAB.Thus,2 =A does not mean the same thing as A =2.Pro- grammers often say"A gets 2"when referring to statements such as A -2 to indicate that they are referring to a variable assignment rather than to a conventional mathematical equation. In MATLAB,variable names,program names,and other file names are case sensitive. Consequently,if you query MATLAB about the value of A.you can get a satisfying.if not terribly exciting.result: Code 2.3.3: 1>>A Output2.3.3: A= 2
26 Interacting With MATLAB or are part of a file in the Editor. Therefore, typing commands into the Command window can be a good way to experiment with getting the syntax right before you add the lines to an edited program. One of the most fundamental programming tasks is to assign a value to a variable. Suppose you want to assign the number 2 to some variable, arbitrarily called A . This can be done by typing A = 2 after the command line prompt as follows: Code 2.3.1: >> A = 2 Output 2.3.1: A = 2 The ordering of terms in the assignment is important, as shown below. Code 2.3.2: >> 2 = A Output 2.3.2: ??? 2 = A Error: The expression to the left of the equals sign is not a valid target for an assignment. The error message indicates that, in contrast to mathematics, where an equation means the same thing regardless of whether terms appear to the left or right of the equal sign, order matters in MATLAB. Thus, 2 = A does not mean the same thing as A = 2 . Programmers often say „A gets 2‰ when referring to statements such as A = 2 to indicate that they are referring to a variable assignment rather than to a conventional mathematical equation. In MATLAB, variable names, program names, and other file names are case sensitive. Consequently, if you query MATLAB about the value of A , you can get a satisfying, if not terribly exciting, result: Code 2.3.3: >> A Output 2.3.3: A = 2