MATLAB Compiler    

Main Routine Written in M-Code

If your main routine is an M-file, you must:

Registering the print handler requires several steps, some performed in C and some in M-code. To register a print handler from your main M-file, you call a dummy print handler initialization function written in M-code. The MATLAB Compiler translates that call into a call to the actual print handler initialization function written in C or C++.

To set up for this translation, you must write two print handler initialization functions:

You call the dummy print handler initialization function from your main M-file. The MATLAB Compiler translates that call into a call to your print handler initialization function written in C or C++.

Example Files

In this example, two M-files and one C file are built into a stand-alone application. The main routine is mr.m.

Writing the Print Handler in C/C++

First, write a print handler in C following the standard rules for a print handler: it must take one argument of type const char *s and return void.

The print handler in this example is very simple.

The file myph.c contains this code.

Registering the Print Handler

Registering the print handler requires several steps, some performed in C and some in M. Be careful to name your C and M print handler initialization functions according to the rules presented below. Otherwise, the correspondence between the two is missing.

Naming the Print Handler Initialization Routine in C.   When you write the print handler initialization routine in C, you must follow the naming convention used by the MATLAB C Math Library. This name will appear in a header file that is generated by the MATLAB Compiler when it compiles the stub M-function, initprnt in this example. See the earlier section, "Interfacing M-Code to C/C++ Code," for more information.

You should include this Compiler-generated header file when you define the C function. For example, the print handler initialization routine developed here is called MInitprnt and is found in mr_external.h.

Naming the Dummy Print Handler Initialization Routine in M-Code.   When you name the dummy print handler initialization routine in M-code, you must name it after the base part of the actual print handler initialization routine (the one written in C or C++).

For example, the dummy print handler initialization routine shown here is called initprnt.

Writing the Initialization Routine in C.   First, write the print handler initialization routine in C. All print handler initialization functions register the name of the print handler function by calling mlfSetPrintHandler, passing a pointer to the print handler (the function name) as an argument.

Your initialization function must take no arguments and return void. For example,

The file myph.c contains this code.

Writing a Dummy Initialization Function in M-Code.   Next, write the dummy print handler initialization routine in M-code. The body of this function is empty, but without the function declaration, the MATLAB Compiler can't successfully translate the call to initprnt in M-code into a call to MInitprnt() in C.

The function can be placed in the same M-file that defines the main mr.m in this example. It is declared as function initprnt and contains the %#external pragma.

Initializing the Print Handler in Your Main M-File.   Call the dummy print handler initialization routine in the first executable line of your main M-file. For example, in mr.m the call to initprnt immediately follows the function declaration.

Building the Executable

You must compile myph.c with one of the supported C compilers, and you must ensure that the resulting object file is linked into the stand-alone application. To build the C stand-alone executable, at the DOS/UNIX prompt type

Testing the Executable

Run the executable by typing at the MATLAB prompt

    The output displays as


 Main Routine Written in C Optimizing Performance