Products & Services Industries Academia Support User Community Company

Learn more about MATLAB Compiler   

Coding with M-Files Only

M-File Advantages

One way to create a standalone 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 with M 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 standalone 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.

function r = mrank(n)
r = zeros(n,1);
for k = 1:n
   r(k) = rank(magic(k));
end

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

main.m

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

function main
r = mrank(5)

Compiling the Example

To compile these functions into code that can be built into a standalone application, invoke MATLAB Compiler.

mcc -m main mrank

The -m option causes MATLAB Compiler to generate C source code suitable for standalone applications. For example, MATLAB Compiler generates C source code files main_main.c and main_mcc_component_data.c. main_main.c contains a C function named main; main_mcc_component_data.c contains data needed by the MCR to run the application.

To build an application, you can use mbuild to compile and link these files. Or, you can automate the entire build process (invoke 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

mcc -m main mrank

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

mcc -mc main mrank

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

mcc -mv main mrank

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

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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