User`s guide
4 Stand-Alone Applications
4-42
Mixing M-Files and C or C++
The examples in this section illustrate how to mix M-files and C or C++ source
code files:
• The first example is a simple application that mixes M-files and C code.
• The second example illustrates how to write C code that calls a compiled
M-file.
One way to create a stand-alone application is to code some of it as one or more
function M-files and to code other parts directly in C or C++. To write a
stand-alone application this way, you must know how to
• Call the external C or C++ functions generated by the MATLAB Compiler.
• Handle the results these C or C++ functions return.
Note If you include compiled M code into a larger application, you must
produce a library wrapper file even if you do not actually create a separate
library. For more information on creating libraries, see the library sections in
“Supported Executable Types” on page 5-21.
Simple Example
This example involves mixing M-files and C code. Consider a simple
application whose source code consists of
mrank.m and mrankp.c.
mrank.m
mrank.m contains a function that returns a vector of the ranks of the magic
squares from 1 to
n:
function r = mrank(n)
r = zeros(n,1);
for k = 1:n
r(k) = rank(magic(k));
end