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
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.
Script files are usually created with a plain text editor. You can also use the MATLAB diary command to record commands as you type
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.
diary command is used to record
an interactive session. To start recording type
>> diary filenamewhere ``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 offIn other words a typical session would be
>> diary myDiary >> (some MATLAB commands) (some MATLAB output) >> diary offThe 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.
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.
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]