Page 38 - MATLAB an introduction with applications
P. 38
MATLAB Basics ——— 23
1.15 SCRIPT FILES
A script is a sequence of ordinary statements and functions used at the command prompt level. A script is
invoked the command prompt level by typing the file-name or by using the pull down menu. Scripts can also
invoke other scripts.
The commands in the Command Window cannot be saved and executed again. Also, the Command
Window is not interactive. To overcome these difficulties, the procedure is first to create a file with a list of
commands, save it and then run the file. In this way, the commands contained are executed in the order they
are listed when the file is run. In addition, as the need arises, one can change or modify the commands in the
file; the file can be saved and run again. The files that are used in this fashion are known as script files. Thus,
a script file is a text file that contains a sequence of MATLAB commands. Script file can be edited (corrected
and/or changed) and executed many times.
1.15.1 Creating and Saving a Script File
Any text editor can be used to create script files. In MATLAB, script files are created and edited in the Editor/
Debugger Window. This window can be opened from the Command Window. From the Command Window,
select File, New and then M-file. Once the window is open, the commands of the script file are typed line by
line. The commands can also be typed in any text editor or word processor program and then copied and
pasted in the Editor/Debugger Window. The second type of M-files is the function file. Function file enables
the user to extend the basic library functions by adding ones own computational procedures. Function
M-files are expected to return one or more results. Script files and function files may include reference to other
MATLAB toolbox routines.
MATLAB function file begins with a header statement of the form:
function (name of result or results) = name (argument list)
Before a script file can be executed it must be saved. All script files must be saved with the extension “.m”.
MATLAB refers to them as M-files. When using MATLAB M-files editor, the files will automatically be saved
with a “.m” extension. If any other text editor is used, the file must be saved with the “.m” extension, or
MATLAB will not be able to find and run the script file. This is done by choosing Save As… from the File
menu, selecting a location, and entering a name for the file. The names of user defined variables, predefined
variables, MATLAB commands or functions should not be used to name script files.
1.15.2 Running a Script File
A script file can be executed either by typing its name in the Command Window and then pressing the Enter
key, directly from the Editor Window by clicking on the Run icon. The file is assumed to be in the current
directory, or in the search path.
1.15.3 Input to a Script File
There are three ways of assigning a value to a variable in a script file.
1. The variable is defined and assigned value in the script file.
2. The variable is defined and assigned value in the Command Window.
3. The variable is defined in the script file, but a specified value is entered in the Command Window
when the script file is executed.
F:\Final Book\Sanjay\IIIrd Printout\Dt. 10-03-09