Pass argument by reference as read input or write output
[y =] coder.ceval('function_name', coder.ref(arg), ... un)
[y =] coder.ceval('function_name', coder.ref(arg), ... un) passes the variable arg by reference as an input or an output to the external C/C++ function called in coder.ceval. You add coder.ref inside coder.ceval as an argument to function_name. The argument list can contain multiple coder.ref constructs. Add a separate coder.ref construct for each argument that you want to pass by reference to function_name.
Only use coder.ref in MATLAB® code that you have compiled with codegen. coder.ref generates an error in uncompiled MATLAB code.
In the following example, a MATLAB function fcn has a single input u and a single output y. fcn calls a C function my_fcn, passing u by reference as an input. The value of output y is passed to fcn by the C function through its return statement.
Here is the MATLAB function code:
function y = fcn(u) %#codegen y = 0; %Constrain return type to double y = coder.ceval('my_fcn', coder.ref(u));
The C function prototype for my_fcn must be as follows:
double my_fcn(double *u)
In this example, the generated code infers the type of the input u from the codegen argument.
The C function prototype defines the input as a pointer because it is passed by reference.
The generated code cannot infer the type of the output y, so you must set it explicitly—in this case to a constant value 0 whose type defaults to double.