Compiler Tips

Calling Built-In Functions from C or C++

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);

Calling a Function from the Command Line

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:

Using MAT-Files in Deployed Applications

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.

Compiling a GUI That Contains an ActiveX Control

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.

Debugging MATLAB Compiler Generated Executables

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.

Deploying Applications That Call the Java Native Libraries

If your application interacts with Java, you need to specify the search path for native method libraries by editing librarypath.txt and deploying it.

  1. Copy librarypath.txt from matlabroot/toolbox/local/librarypath.txt.

  2. 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.

  3. Edit librarypath.txt by adding the directory that contains the native library that your application's Java code needs to load.

Locating .fig Files in Deployed Applications

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 That Creates Figures and Terminating Figures by Force

Blocking Execution of a Console Application with the mclWaitForFiguresToDie 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:

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.

Terminating Figures by Force with the mclKillAllFigures Method

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:

Passing Arguments to and from a Standalone 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:

Passing Arguments to a Double-Clickable Application

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.

Using Graphical Applications in Shared Library Targets

When deploying a GUI as a shared library to a C/C++ application, use mclWaitForFiguresToDie to display the GUI until it is explicitly terminated.

Using the VER Function in a Compiled MATLAB Application

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.

  


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