MATLAB Compiler Previous page   Next Page

Coding with M-Files Only

One way to create a stand-alone application is to write all the source code in one or more M-files or MEX-files as in the previous magic square example. Coding an application in M-files allows you to take advantage of the MATLAB interactive development environment. Once the M-file version of your program works properly, compile the code and build it into a stand-alone application.

Example

Consider a simple application whose source code consists of two M-files, mrank.m and main.m. This example generates C code from your M-files.

mrank.m

mrank.m returns a vector of integers, r. Each element of r represents the rank of a magic square. For example, after the function completes, r(3) contains the rank of a 3-by-3 magic square.

In this example, the line r = zeros(n,1) preallocates memory to help the performance of the MATLAB Compiler.

main.m

main.m contains a "main routine" that calls mrank and then prints the results.

Compiling the Example

To compile these into code that can be built into a stand-alone application, invoke the MATLAB Compiler.

The -m option causes the MATLAB Compiler to generate C source code suitable for stand-alone applications. For example, the MATLAB Compiler generates C source code files main.c, main_main.c, and mrank.c. main_main.c contains a C function named main; main.c and mrank.c contain C functions named mlfMain and mlfMrank.

To build an executable application, you can use mbuild to compile and link these files. Or, you can automate the entire build process (invoke the MATLAB Compiler on both M-files, use mbuild to compile the files with your ANSI C compiler, and link the code) by using the command

If you need to combine other code with your application (Fortran, for example, a language not supported by the MATLAB Compiler), or if you want to build a makefile that compiles your application, you can use the command

The -c option inhibits invocation of mbuild. You will probably need to examine the verbose output of mbuild to determine how to set the compiler options in your makefile. Run

to see the switches and options that mbuild uses on your platform.


Previous page  Running the Application Mixing M-Files and C or C++ Next page

© 1994-2005 The MathWorks, Inc.