User`s guide

Mixing M-Files and C or C++
4-37
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 cod e into a larger application, you must
produce a libra ry wrapper file e ven if you do not a ctually create a s epara te
library. For more information on creating libraries, see the library sections in
“Supported Executable Types” in Chap te r 5.
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