User`s guide
Coding with M-Files Only
Coding with M-Files Only
In this section...
“M-File Advantages” on page 6-11
“Example” on page 6-11
M-File Advantages
One way to create a standalone application is to w rite all the source code in
one or more M -files o r M EX-file s as i n the previo us mag ic 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 w orks properly, compile the code and build it into a standalone
application.
Example
Consider a simple application w hose source code consis ts of tw o M-files,
mrank.m and main.m. This example gener ates C code from your M -files.
mrank.m
mrank.m returns a vector of integers, r.Eachelementofr represents the rank
of a magic square. For example, after the function completes,
r(3) contains
the rank of a 3-by-3 magic sq uare.
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 Compile r.
main.m
main.m contains a “main routine” that calls mrank and then prints the results.
6-11