User`s guide

Interfacing M-Code to C/C++ Code
5-47
private, or method). When using this pragma, the Compiler will generate an
additional header file called
file_external.h or file_external.hpp, where
file is the name of the initial M-file containing the %#external pragma. This
header file will contain the
extern declaration of the function that the user
must provide. This function must conform to the same interface as the
Compiler-generated code.
The Compiler will still generate a
.c or .cpp file from the .m file in question.
The Compiler will generate the
feval table, which includes the function and
all of the required interface functions for the M-function, but the body of
M-code from that function will be ignored. It will be replaced by the
hand-written code. The Compiler will generate the interface for any functions
that contain the
%#external pragma into a separate file called
file_external.h or file_external.hpp. The Compiler-generated C or C++
file will include this header file to get the declaration of the function being
provided.
In this example, place the pragma in the
collect_one local function:
function collect
y = zeros(1, 100); % pre-allocate the matrix
for i = 1:100
y(i) = collect_one;
end
function y = collect_one
%#external
persistent t;
if (isempty(t))
t = 0;
end
t = t + 0.05;
end
y = sin(t);
When this file is compiled, the Compiler creates the additional header file
collect_external.h, which contains the interface between the
Compiler-generated code and your code. In this example, it would contain
extern mxArray * Mcollect_collect_one(int nargout_);