Script Files

Converting Script M-Files to Function M-Files

MATLAB provides two ways to package sequences of MATLAB commands:

Some things to remember about script and function M-files:

MATLAB Compiler can compile script M-files or can compile function M-files that call scripts. You can either specify an script M-file explicitly on the mcc command line, or you can specify function M-files that include scripts.

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.

Running this script M-file from a MATLAB session creates variables m and t in your MATLAB workspace browser.

If desired, convert this script M-file into a function M-file by simply adding a function header line.

function houdini(sz)
m = magic(sz); % Assign magic square to m.
t = m .^ 3;    % Cube each element of m.
disp(t)        % Display the value of t.

MATLAB Compiler can now compile houdini.m. However, because this makes houdini a function, running the function no longer creates variables m and t in the MATLAB workspace browser. If it is important to have m and t accessible from the MATLAB workspace browser, you can change the beginning of the function to

function [m,t] = houdini(sz)

The function now returns the values of m and t to its caller.

Including Script Files in Deployed Applications

Compiled applications consist of two layers of M-files. The top layer is the interface layer and consists of those functions that are directly accessible from C or C++.

In standalone applications, the interface layer consists of only the main M-file. In libraries, the interface layer consists of the M-files specified on the mcc command line.

The second layer of M-files in compiled applications includes those M-files that are called by the functions in the top layer. You can include scripts in the second layer, but not in the top layer.

For example, you could produce an application from the houdini.m script M-file by writing a new M-function that calls the script, rather than converting the script into a function.

function houdini_fcn
	houdini;

To produce the houdini_fcn , which will call the houdini.m script M-file, use

mcc -m houdini_fcn
  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS