Main Content

Specify Buffer Reuse for MATLAB Function Blocks in a Path

You can specify buffer reuse across MATLAB Function blocks by using the same variable name for the input and output arguments. The code generator tries to reuse the output of one MATLAB Function block as the input to the next MATLAB Function block. This optimization conserves RAM and ROM consumption and reduces data copies.

Example Model

  1. Use Inport, Outport, and MATLAB Function blocks to create the model mf_inplace.

  2. Open each MATLAB Function block and copy the following code:

    function y = fcn(y)
    %#codegen
    
    y=y+4;
    
  3. Open the Configuration Parameters dialog box. On the Code Generation tab, change the System target file to ert.tlc.

  4. On the Solver tab, change the Type parameter to Fixed-step.

Generate Code with Optimization

Generate code for the model. The mf_inplace.c file contains this code:

void mf_inplace_MATLABFunction(real_T *rty_y)
{
  *rty_y += 4.0;
}
 
void mf_inplace_step(void)
{
  real_T rtb_y_p5;
  rtb_y_p5 = mf_inplace_U.In1;
  mf_inplace_MATLABFunction(&rtb_y_p5);
  mf_inplace_MATLABFunction(&rtb_y_p5);
  mf_inplace_MATLABFunction(&rtb_y_p5);
  mf_inplace_Y.Out1 = rtb_y_p5;
  mf_inplace_MATLABFunction(&mf_inplace_Y.Out1);
}
The code generator reuses the variable rtb_y_p5 for the input and output arguments of each MATLAB Function block.

Note

On the Code Generation tab in the Subsystem Block Parameters dialog box, if the Function packaging parameter is set to Nonreusable function and the Function interface parameter is set to Allow arguments (Optimized), the code generator cannot reuse the input and output arguments.

Related Topics