| MATLAB Compiler | ![]() |
Interfacing M-Code to C/C++ Code
The MATLAB Compiler supports calling arbitrary C/C++ functions from your M-code. You simply provide an M-function stub that determines how the code will behave in M, and then provide an implementation of the body of the function in C or C++.
C Example
Suppose you have a C function that reads data from a measurement device. In M-code, you want to simulate the device by providing a sine wave output. In production, you want to provide a function that returns the measurement obtained from the device. You have a C function called measure_from_device() that returns a double, which is the current measurement.
collect.m contains the M-code for the simulation of your application.
function collect y = zeros(1, 100); %Preallocate the matrix for i = 1:100 y(i) = collect_one; end function y = collect_one persistent t; if (isempty(t)) t = 0; end t = t + 0.05; y = sin(t);
The next step is to replace the implementation of the collect_one function with a C implementation that provides the correct value from the device each time it is requested. This is accomplished by using the %#external pragma.
The %#external pragma informs the MATLAB Compiler that the function will be hand written and will not be generated from the M-code. This pragma affects only the single function in which it appears. Any M-function may contain this pragma (local, global, private, or method). When using this pragma, the Compiler will generate an additional header file called fcn_external.h, where fcn is the name of the initial M-function containing the %#external pragma. This header file will contain the extern declaration of the function that you must provide. This function must conform to the same interface as the Compiler-generated code.
The Compiler will generate the interface for any functions that contain the %#external pragma into a separate file called fcn_external.h. 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); % preallocate 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_one_external.h, which contains the interface between the Compiler-generated code and your code. In this example, it would contain
We recommend that you include this header file when defining the function. This function could be implemented in this C file, measure.c, using the measure_from_device() function.
#include "collect_one_external.h" #include <math.h> extern double measure_from_device(void); void collect_one(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]); { plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); *(mxGetPr(plhs[0])) = measure_from_device() } double measure_from_device(void) { static double t = 0.0; t = t + 0.05; return sin(t); }
In general, the Compiler will use the same interface for this function as it would generate. To generate the application, use
| C++ Library Wrapper | Using Pragmas | ![]() |
© 1994-2005 The MathWorks, Inc.