User`s guide

Script Files
Script Files
In this section...
“Converting S cript M-Files to Function M-Files” on page 5-19
“Including Script Files in Deployed Applications” on page 5-20
Converting Script M-Files to Function M-Files
MATLAB provides two ways to package sequences of MATLAB commands:
Function M-files
Script M-files
These tw o categories of M-files differ in three important respects:
You can pass argum ents to function M-files, but not to script M-files.
Variables used inside function M-files are local to that function; you cannot
access these variables from the MATLAB interpreter’s workspace unless
they are passed back by the function. By contrast, variables u sed inside
script M-files are shared with the caller’s workspace; you can access these
variables from the MATLAB interpreter command line.
Variables that are declared as persistent in a MEX-file m ay not retain their
values through multiple calls from MATLAB.
MATLAB Compiler cannot compile scri pt M-files, however, it can compile
function M-files that call scripts. You cannot specify a script M-file explicitly
on the
mcc command line, but you can specify function M-files that include
scripts themselves.
Converting a script into a function is usually fairly simple. To convert a script
to a function, simply add a
function line at the top of the M-file.
Forexample,considerthescriptM-file
houdini.m:
m = magic(4); % Assign 4x4 magic square to m.
t = m .^ 3; % C ube each element of m.
disp(t); % Display the value of t.
5-19