Package: coder

Pass argument by reference as read-only input


[y =] coder.ceval('function_name', coder.rref(argI), ... un)



Variable passed by reference as a read-only input to the external C/C++ function called in coder.ceval.


[y =] coder.ceval('function_name', coder.rref(argI), ... un) passes the variable argI by reference as a read-only input to the external C/C++ function called in coder.ceval. You add coder.rref inside coder.ceval as an argument to function_name. The argument list can contain multiple coder.rref constructs. Add a separate coder.rref construct for each read-only argument that you want to pass by reference to function_name.

    Caution   The generated code assumes that a variable passed by coder.rref is read-only and is optimized accordingly. Consequently, the C/C++ function must not write to the variable or results can be unpredictable.

Only use coder.rref in MATLAB® code that you have compiled with codegen. coder.rref 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 foo, passing u by reference as a read-only 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('foo', coder.rref(u));

The C function prototype for foo must be as follows:

double foo(const 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.

Introduced in R2011a

Was this topic helpful?