MATLAB(TM) Hypertext Reference, Copyright (c) 1995 Gerald Recktenwald, All rights reserved

MATLAB Scripts

Scripts are collections of MATLAB commands stored in plain text files. When you type the name of the script file at the MATLAB prompt the commands in the script file are executed as if you had typed them in from the keyboard. Script files must end with the extension ``.m'' (for example ``myScript.m''), and often these files are referred to as m-files.

Apart from syntax errors, perhaps the most common problem with using script files is that MATLAB cannot locate the script file you wish to execute. This error can be very frustrating to inexperienced MATLAB users because MATLAB gives the error message

	??? Undefined function or variable ...
and it is not immediately clear why the function is undefined when its m-file is clearly visible to the user. The solution to this is to make sure that MATLAB's internal path variable is set to the direction containing the script. Refer to the ``Setting the MATLAB Path'' of this reference for help on setting the path.

Here's an outline of this section

Scripts versus Functions

Scripts are m-files containing MATLAB statements. MATLAB ``functions'' are another type of m-file. The biggest difference between scripts and functions is that functions have input and output parameters. Script files can only operate on the variables that are hard-coded into their m-file. As you can see, functions much more flexible. They are therefore more suitable for general purpose tasks that will be applied to different data. Scripts are useful for tasks that don't change. They are also a way to document a specific sequence of actions, say a function call with special parameter values, that may be hard to remember.

There are more subtle differences between scripts and functions. A script can be thought of as a keyboard macro: when you type the name of the script, all of the commands contained in it are executed just as if you had typed these commands into the command window. Thus, all variables created in the script are added to the workspace for the current session. Furthermore, if any of the variables in the script file have the same name as the ones in your current workspace, the values of those variables in the workspace are changed by the actions in the script. This can be used to your advantage. It can also cause unwanted side effects.

In contrast, function variables are local to the function. (The exception is that it's possible to declare and use global variables, but that requires and explicit action by the user.) The local scope of function variables gives you greater security and flexibility. The only way (besides explicitly declared global variables) to get information into and out of a function is through through the variables in the parameter lists.

Creating a script m-file

Script files are usually created with a plain text editor. You can also use the MATLAB diary command to record commands as you type

Creating scripts with a (plain) text editor

MATLAB m-files must be plain text files, i.e. files with none of the special formatting characters included by default in files created by a word-processors. Most word-processors provide the option of saving the file as plain text, (look for a ``Save As...'' option in the file menu). A word-processor is overkill for creating m-files, however, and it is usually more convenient to use a simple text editor, or a ``programmer's editor'' to create m-files. Most computer systems have several text editors, though if you are running MATLAB on a personal computer only one of these may be installed.

Most of the time when you are writing m-files you want to have the text editor and MATLAB open at the same time. Since modern word-processors require lots of system RAM it may not even be possible for you to use one for m-file development. In this case a plain text editor will be your only option for m-file development.

Recording a script with the diary command

The MATLAB diary command is used to record an interactive session. To start recording type

	>> diary filename	
where ``filename'' is the name of the file to which the diary command copies all of the commands enterred and all of the resulting output. To stop recording type
	>> diary off	
In other words a typical session would be
	>> diary myDiary

	>>   (some MATLAB commands)
		(some MATLAB output)

	>> diary off
The result is a plain text file called ``myDiary'' in the current working directory.

A diary file can be an easy way to create a script (or function) m-file. After you've recorded the diary you delete the MATLAB output, including any error messages and incorrect commands, and save the result as another file.

Make sure MATLAB knows the path to your script

MATLAB cannot execute a script unless it knows where to find its m-file. This requires that the script be in the internal MATLAB path. Refer to ``Setting the MATLAB Path'' for more information.

Using and abusing scripts

I suppose it's only a matter of personal preference, but I rarely use script files for complex numerical calculations.

Scripts are useful for setting global behavior of a MATLAB session. This includes any terminal settings for a remote serial line, or setting the parameters of the Figure window for a certain size plot.

Sometimes a script is a useful starting point in developing a MATLAB function. When I'm starting to write a new function, but I'm uncertain about the command syntax or the actual sequence of commands I want to use, I'll use the diary command to record my tests. After I get the correct command sequence I close the diary file and open it with a text editor. This gives me a jump start on the function development because I don't have to re-enter the commands. I just delete the incorrect lines, add the function definition, and insert variables.

I've witnessed inexperienced MATLAB users who have become dependent on scripts when functions would ultimately be easier to use. This typically occurs when someone gets stuck on the function definition syntax, especially on the use of input and output parameters. Rather than figure out how to properly pass parameters to the function they repeatedly edit their script files to simulate the effect of variable arguments. I hope this hypertext reference can help you avoid that fate. The Programming Basics section contains several example function files that you can use as starting points for your own functions (or scripts).

There is nothing wrong with using scripts, of course. Scripts and functions are two tools for working with MATLAB programming. Using the appropriate tool for the job will help you achieve your analysis goals more easily.

Side effects on the MATLAB workspace

Scripts can have unwanted side effects on the MATLAB workspace used in an interactive session.

Excessive reliance on scripts can result in a confusing mix of variables in the MATLAB workspace. This workspace is the collection of variables in the current interactive session. Because script variables are added to the workspace it is quite easy, in a long sequence of calculations, to a have variable changed by the script. Sometimes this side effect is immediately obvious, for example a vector that you had been using is reassigned as a scalar, leading to a syntax error. Other times only a numerical value is changed. This later situation is obviously dangerous and can lead to subtle errors in your calculation.

Preceding Section Master Outline Section Outline Next Section

[Preceding Section] [Master Outline] [Section Outline] [Next Section]