MATLAB Compiler    

Main Routine Written in C

If your main routine is coded in C (as opposed to being written as an M-file), you must:

This section references source code from a sample stand-alone application written for Microsoft Windows. The main routine WinMain is written in C. The source code illustrates how to register and write a print handler.

The application is built from two files:

Both mrankwin.c and mrank.m are located in the
<matlab>/extern/examples/compiler/ directory of your installation.

The WinMain routine in mrankwin.c is straightforward:

The first and last items in this list refer to print handlers.

Registering a Print Handler

To register a print handler routine, call the MATLAB C Math Library routine mlfSetPrintHandler as the first executable line in WinMain (or main). mlfSetPrintHander takes a single argument, a pointer to a print handler function.

For example, the first line of WinMain in mrankwin.c registers the print handler routine named WinPrint by calling mlfSetPrintHandler.

Writing a Print Handler

Whenever an mlf function within a stand-alone application makes a request to write data, the application automatically intercepts the request and calls the registered print handler, passing the text to be displayed. In fact, the application calls the print handler once for every line of data to be output.

The print handler that you write must:

The print handler routine WinPrint in the example program illustrates one possible approach to writing a print handler for a Windows program.

When the example WinMain routine prints the array returned by mlfMrank,

the registered print handler, WinPrint, is called. If array R contains a 12-row result from the call to mlfMrank, the application calls WinPrint 12 times, each time passing the next line of data. The print handler WinPrint dynamically allocates a buffer to hold the printable contents of array R and appends each text string passed to it to the buffer.

In this design, the print handler prints to a buffer rather than the screen. A companion function WinFlush actually displays the 12 lines of data in a Windows message box.

In the example, WinMain calls WinFlush immediately following the call to mlfPrintMatrix.

Though WinFlush is not part of the print handler, this implementation of a print handler requires that you call WinFlush after any mlf function that causes a series of calls to the print handler. For this short program, this design is appropriate.

Here is the source code from mrankwin.c for WinPrint and WinFlush. It includes:

For more details on mlfPrintMatrix, see the MATLAB C Math Library User's Guide.


 Print Handlers Main Routine Written in M-Code