MATLAB C/C++ Graphics Library | ![]() ![]() |
Compiling Applications Written as Scripts
The Compiler cannot compile applications written as scripts because scripts interact with the MATLAB base workspace, and stand-alone applications do not have access to the MATLAB base workspace.
Symptom
If you attempt to compile a script, the Compiler outputs the error message
??? Error: File "filename
" is a Script M-file and cannot be
compiled with the current Compiler.
where filename
is the name of your script M-file.
Workaround
To compile an application written as a script, turn it into a MATLAB function. To do this, include the MATLAB function prototype at the top of the file. You must also find where the script depends on variables in the base workspace and declare these variables as global variables.
For example, in the following script, the variable f
, set by the call to the figure
function, exists in the base workspace. This variable is then passed as a parameter to the function, my_func
, specified in the callback property string. Passing a workspace variable in a callback string is not supported by the MATLAB Compiler.
f = figure; p_btn = uicontrol(gcf,... 'style', 'pushbutton',... 'Position',[10 10 133 25 ],... 'String', 'Press Here',... 'CallBack','my_func(f);');
The following example shows this script transformed into a function.
function was_a_script() % new function global f; f = figure; p_btn = uicontrol(gcf,... 'style', 'pushbutton',... 'Position',[10 10 133 25 ],... 'String', 'Press Here',... 'CallBack','my_callback');
In this code example, note the following:
f
, formerly referenced in the base workspace, as a global variable. This makes it accessible to the callback routine. my_func
in the callback string with the name of a new function, my_callback
. This new function performs the processing formerly done in the callback string. Here is the new callback function. Note how the function also declares f
as
a global variable.
function my_callback() % revised callback global f; my_func(f);
![]() | Using Unsupported MATLAB 6.0 Features | Fixing Callback Problems: Missing Functions | ![]() |