| MATLAB® Compiler™ | ![]() |
| On this page… |
|---|
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++.
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, so you provide a function that returns the measurement obtained from the device. For example, this C function, measure_from_device(), 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 disp (y) function y = collect_one persistent t; if (isempty(t)) t = 0; end t = t + 0.05; y = sin(t);
To replace the implementation of the collect_one function with a C implementation, use the %#external pragma.
This pragma informs 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).
Compile the MATLAB code with the %#external pragma once to generate the header file function_name_external.h, where function_name 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 code generated by MATLAB Compiler.
Note If you compile a program that contains the %#external pragma, you must explicitly pass each file that contains this pragma on the mcc command line. |
MATLAB Compiler will generate the interface for any functions that contain the %#external pragma into a separate file called function_name_external.h. The C or C++ file generated by MATLAB Compiler 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 disp (y) function y = collect_one %#external persistent t; if (isempty(t)) t = 0; end t = t + 0.05; y = sin(t);
When this file is compiled, MATLAB Compiler creates the additional header file collect_one_external.h, which contains the interface between MATLAB Compiler generated code and your code. In this example, it would contain:
extern bool mlxCollect_one(int nlhs, mxArray *plhs[],
int nrhs, mxArray *prhs[]);
Note The return type has changed from void to bool in MATLAB Compiler post-R13. |
It is recommended 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);
bool mlxCollect_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);
}
To generate the application, use
mcc -m collect.m measure.c
Note For information on the mxArray, see the External Interfaces documentation. |
![]() | Using Wrapper Files | Overriding Default CTF Archive Embedding Using the MCR Component Cache | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |