| MATLAB® Compiler™ | ![]() |
To enable a C or C++ program to call a built-in function directly, you must write an M-file wrapper around each built-in function you want to access outside of MATLAB. This is necessary because there are no C callable interfaces to built-in functions. For example, to use the magic function in a deployed application, you can use this M-file:
function m = magicsquare(n)
%MAGICSQUARE generates a magic square matrix of size specified
% by the input parameter n.
% Copyright 2003 The MathWorks, Inc.
if (ischar(n))
n=str2num(n);
end
m = magic(n);
You can make a MATLAB function into a standalone that is directly callable from the system command line. All the arguments passed to the MATLAB function from the system command line are strings. Two techniques to work with these functions are:
Modify the original MATLAB function to test each argument and convert the strings to numbers.
Write a wrapper MATLAB function that does this test and then calls the original MATLAB function.
For example:
function x=foo(a, b) if (isstr(a)), a = str2num(a), end; if (isstr(b)), b = str2num(b), end; % The rest of your M-code here...
You only do this if your function expects numeric input. If your function expects strings, there is nothing to do because that's the default from the command line.
To use a MAT-file in a deployed application, use the MATLAB Compiler -a option to include the file in the CTF archive. For more information on the -a option, see -a Add to Archive.
When you save a GUI that contains ActiveX components, GUIDE creates a file in the current directory for each such component. The file name consists of the name of the GUI followed by an underscore (_) and activexn, where n is a sequence number. For example, if the GUI is named ActiveXcontrol then the file name would be ActiveXcontrol_activex1. The file name does not have an extension.
If you use MATLAB Compiler mcc command to compile a GUIDE-created GUI that contains an ActiveX component, you must use the -a option to add the ActiveX control files that GUIDE saved in the current directory to the CTF archive. Your command should be similar to
mcc -m mygui -a mygui_activex1
where mygui_activex1 is the name of the file. If you have more than one such file, use a separate -a option for each file.
As of MATLAB Compiler 4, it is no longer possible to debug your entire program using a C/C++ debugger; most of the application is M-code, which can only be debugged in MATLAB. Instead, run your code in MATLAB and verify that it produces the desired results. Then you can compile it. The compiled code will produce the same results.
If your application interacts with Java, you need to specify the search path for native method libraries by editing librarypath.txt and deploying it.
Copy librarypath.txt from matlabroot/toolbox/local/librarypath.txt.
Place librarypath.txt in <mcr_root>/<ver>/toolbox/local.
<mcr_root> refers to the complete path where the MCR library archive files are installed on your machine.
Edit librarypath.txt by adding the directory that contains the native library that your application's Java code needs to load.
MATLAB Compiler locates .fig files automatically when there is an M-file with the same name as the .fig file in the same directory. If the .fig file does not follow this rule, it must be added with the -a option.
Blocking Execution of a Console Application with the mclWaitForFiguresToDie Method
Terminating Figures by Force with the mclKillAllFigures Method
The purpose of mclWaitForFiguresToDie is to block execution of a calling program as long as figures created in encapsulated M-code are displayed. mclWaitForFiguresToDie takes no arguments. Your application can call mclWaitForFiguresToDie any time during execution. Typically you use mclWaitForFiguresToDie when:
There are one or more figures you want to remain open.
The function that displays the graphics requires user input before continuing.
The function that calls the figures was called from main() in a console program.
When mclWaitForFiguresToDie is called, execution of the calling program is blocked if any figures created by the calling object remain open.
Both MATLAB Builder NE and MATLAB Builder JA use mclWaitForFiguresToDie through the use of wrapper methods. See Blocking Execution of a Console Application that Creates Figures in the MATLAB Builder NE User's Guide and Blocking Execution of a Console Application that Creates Figures in the MATLAB Builder JA User's Guide for more details and code fragment examples.
Caution Use caution when calling the mclWaitForFiguresToDie function. Calling this function from an interactive program like Excel can hang the application. This function should be called only from console-based programs. |
mclKillAllFigures finds all open figures and deletes them. This function uses the same internal algorithm to locate open figures as mclWaitForFiguresToDie. The published signature is:
void mclKillAllFigures(HMCRINSTANCE inst)
Typically you use mclKillAllFigures when:
You need to kill figures that are being displayed as the result of a programming problem, such as an endless loop.
You want to ensure all figures are closed before the execution of another application.
To pass input arguments to a MATLAB Compiler generated standalone application, you pass them just as you would to any console-based application. For example, to pass a file called helpfile to the compiled function called filename, use
filename helpfile
To pass numbers or letters (e.g., 1, 2, and 3), use
filename 1 2 3
Do not separate the arguments with commas.
To pass matrices as input, use
filename "[1 2 3]" "[4 5 6]"
You have to use the double quotes around the input arguments if there is a space in it. The calling syntax is similar to the dos command. For more information, see the MATLAB dos command.
The things you should keep in mind for your M-file before you compile are:
The input arguments you pass to your from a system prompt are considered as string input. If, in your M-code before compilation, you are expecting the data in different format, say double, you will need to convert the string input to the required format. For example, you can use str2num to convert the string input to numerical data. You can determine at run time whether or not to do this by using the isdeployed function. If your M-file expects numeric inputs in MATLAB, the code can check whether it is being run as a standalone application. For example:
function myfun (n1, n2) if (isdeployed) n1 = str2num(n1); n2 = str2num(n2); end
You cannot return back values from your standalone application to the user. The only way to return values from compiled code is to either display it on the screen or store it in a file. To display your data on the screen, you either need to unsuppress (do not use semicolons) the commands whose results yield data you want to return to the screen or, use the disp command to display the value. You can then redirect these outputs to other applications using output redirection (> operator) or pipes (only on UNIX systems).
On Windows, if you want to run the standalone application by double-clicking it, you can create a batch file that calls this standalone application with the specified input arguments. Here is an example of the batch file:
rem main.bat file that calls sub.exe with input parameters sub "[1 2 3]" "[4 5 6]" @echo off pause
The last two lines of code keep your output on the screen until you press a key. If you save this file as main.bat, you can run your code with the specified arguments by double-clicking the main.bat icon.
When deploying a GUI as a shared library to a C/C++ application, use mclWaitForFiguresToDie to display the GUI until it is explicitly terminated.
When you use the VER function in a compiled MATLAB application, it will perform with the same functionality as if you had called it from MATLAB. However, be aware that when using VER in a compiled MATLAB application, only version information for toolboxes which the compiled application uses will be displayed.
![]() | Script Files | Standalone Applications | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |