Documentation Center

  • Trial Software
  • Product Updates

coder.wref

Package: coder

Pass argument by reference as write-only output

Syntax

[y =] coder.ceval('function_name', coder.wref(argO), ... un);

Arguments

argO

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

Description

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

    Caution   The generated code assumes that a variable passed by coder.wref is write-only and optimizes the code accordingly. Consequently, the C/C++ function must write to the variable. If the variable is a vector or matrix, the C/C++ function must write to every element of the variable. Otherwise, results are unpredictable.

Only use coder.wref in MATLAB® code that you have compiled with codegen. coder.wref generates an error in uncompiled MATLAB code.

Examples

In the following example, a MATLAB function fcn has a single input u and a single output y, a 5-by-10 matrix. fcn calls a C function init to initialize the matrix, passing y by reference as a write-only output. Here is the MATLAB function code:

function y = fcn(u)
%#codegen
y = zeros(5,10); 
coder.ceval('init', coder.wref(y));

The C function prototype for init must be as follows:

void init(double *x);

In this example:

  • Although the C function is void, coder.wref allows it to access, modify, and return a matrix to the MATLAB function.

  • The C function prototype defines the output as a pointer because it is passed by reference.

  • For C/C++ code generation, you must set the type of the output y explicitly—in this case to a matrix of type double.

  • The generated code collapses matrices to a single dimension.

See Also

| |

Was this topic helpful?