An interactive introduction to MATLAB

(Jeff_L) #1

3 scripts and functions


3.1 Script files


A script file is a text file that contains a series ofMATLABcommands that
you would type at the command prompt. A script file is one type of m-file ( .m
file extension), the other type being a function file which will be examined in
Section 3.2. Script files are useful when you have to repeat a set of commands,
often only changing the value of one variable every time. By writing a script
file you are saving your work for later use. Script files work on variables in the
current workspace, and results obtained from running a script are left in the
current workspace.
New script files can be created by clicking on the New M-File icon in the
MATLABWindow toolbar, shown in Figure 12. This launches theMATLAB
Editor with a blank M-File.


Figure 12: New M-File toolbar icon inMATLABWindow

Listing 3.1 presents the commands from Listing 2.7 in the form of a script
file. The script file has been saved as _mysurf.m , and can be run by either
typingmy_surfat the command prompt, or clicking the Save and run icon
in the Editor Window toolbar, as shown in Figure 13. Copy and paste the
example into a new script file, and run it to see the results for yourself.


Figure 13: Save and run toolbar icon in Editor Window

33
Free download pdf