<< Chapter < Page | Chapter >> Page > |
If you quit MATLAB now, all the commands you have typed will be lost. This is where the Edit window is useful. If you choose new from the File menu, a new window appears with the title Edit 1 : Untitled . In this window, you should type in all the commands you would like MATLAB to execute atonce. When you are finished typing, you may save the file by choosing save or save as in the file menu and by entering a name for the file. If the edit window is active (that is, if it appears in front), then choose save and go from the file menu to save the file and execute it. If the command window is active,then you can execute the file by entering its name.
Editing Files. To test your understanding of file editing, enter the following commands in a file named myfile:
You do not have to type the % sign and the text that follows it. These are simply comments in a file. They are ignored by the MATLAB interpreter.You should, however, make a habit of adding comments (preceded by %) to your file if you want to be able to understand programs that have been writtenlong ago.
Do not forget to save your file. Such a file is called a script file . It contains MATLAB commands that could have been entered one by one in thecommand window. You have three ways to execute a script file:
Try each of these three methods in order to get used to their differences. Figure 1 shows the plot that you should get.
Creating Functions. MATLAB puts many commands at your disposal, and you just have to enter their names (with or without arguments) to execute them. Some commands are built in to MATLAB, and others arecontained in files to which you have access (not to modify them, but to see how things are done). You can try to display the contents of a command filein the command window by entering the command type filename. Enter type plot. You should get the message
This means that the commandis a build-in function and thus cannot
be typed by the user (it is compiled with the program). Similarly, try to typethe function file containing the commandby entering the command. You should get
type sinh
This is a typical example of a
function file . It differs from a script file by the
fact that the first line contains the word. What it does is make the
commandequivalent to the command.
The variableis the input argument (there could be more than one), and
y is the output argument (there also could be more than one). The second line
contains comments about the function and its arguments. They can spreadto several lines. Display them by typing the command. The third
line contains the actual function commands (there could also be several lines).
help sinh
As an exercise, enter the function:
perp(x)
Evaluateon various complex numbers. Replace the last line by;.
Do you get the same result? Why?
y = x*j
Note that local variables are just that–
local . Only the input and out-
put arguments are kept in memory after the function is called and executed.For more on functions and multiple arguments, see the MATLAB manual. In
the previous example, the variable
is local. If, before using the function, you use the same variable
, its contents will not be affected by the
command. Verify this.
perp(z)
Normally, while a file (script or function) is executed, the commands
are not displayed on the command window. Just the results are displayed.The commandallows you to view all the instructions. This is useful for
debugging and demonstrations. It is turned off by typing.
echo off
Notification Switch
Would you like to follow the 'A first course in electrical and computer engineering' conversation and receive update notifications?