L5 Scripts
L5 Scripts
workspace
Scripts
• Scripts are m-files containing MATLAB statements.
• Script files can only operate on the variables that are hard-coded into
their m-file.
• Scripts are useful for tasks that don't change.
• No input and output arguments
• 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.
Scripts
• 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.
Functions
• Functions much more flexible.
• They are more suitable for general purpose tasks.
• MATLAB ``functions'' are another type of m-file.
• Functions have input and output parameters.
• function variables are local to the function.
• 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 the variables in the
parameter lists.
Creating a script m-file
You can load saved variables in command window, by using the load
function.
For example, load all variables from the file wp1.mat
load(‘wp1')
To load variables from the file wp1.mat
load(‘wp1',‘D',‘E')
Load Workspace Variables