Compiler Tips

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:

  • 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 (ischar(a)), a = str2num(a), end;
    	if (ischar(b)), b = str2num(b), end;
    
    % The rest of your MATLAB 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.

Using MAT-Files in Deployed Applications

To use a MAT-file in a deployed application, use the -a option to include the file in the deployable 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 folder 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 the 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 folder to the deployable 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.

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 MATLAB Runtime library archive files are installed on your machine.

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

Locating .fig Files in Deployed Applications

MATLAB Compiler™ and MATLAB Compiler SDK™ locate .fig files automatically when there is a MATLAB file with the same name as the .fig file in the same folder. If the .fig file does not follow this rule, it must be added with the -a option.

Terminating Figures by Force In an Application

The purpose of mclWaitForFiguresToDie is to block execution of a calling program as long as figures created in the deployed application 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.

When mclWaitForFiguresToDie is called, execution of the calling program is blocked if any figures created by the calling object remain open.

Both .NET assemblies and Java packages use mclWaitForFiguresToDie through the use of wrapper methods. See Block Console Display When Creating Figures and Block Execution of Applications that Creates Figures 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.

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 MATLAB file before you compile are:

  • The input arguments you pass to your application from a system prompt are considered as string input. If, in your MATLAB 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 MATLAB 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).

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.

Was this topic helpful?