User`s guide

Interfacing M-Co de to C/C++ Code
Inter facing M -Code to C/C++ Code
In this section...
“Overview” on page 5-13
“C Ex ample” on page 5-13
Overview
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 be have in M, and then provide an implementation of the body of the
function in C or C++.
CExample
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 meas urement obtained from the device. You have a C function called
measure_from_device() that returns a double, which is the current
measurement.
collect.m containstheM-codeforthesimulation 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
5-13