Main Content

Use Dynamic Memory Allocation for Variable-Size Arrays in aMATLAB FunctionBlock

This example shows how to use dynamic memory allocation for variable-size arrays in aMATLAB Functionblock. Dynamic memory allocation allocates memory on the heap as needed at run time, instead of allocating memory statically on the stack. Dynamic memory allocation is beneficial when:

  • You do not know the upper bound of an array.

  • You do not want to allocate memory on the stack for large arrays.

You can use dynamic memory allocation only for arrays that are local to theMATLAB Functionblock.

You cannot use dynamic memory allocation for:

  • Input and output signals. Variable-size input and output signals must have an upper bound.

  • Parameters or global variables. Parameters and global variables must be fixed-size.

  • Fields of bus arrays. Bus arrays cannot have variable-size fields.

  • Discrete state properties of System objects associated with aMATLAB Systemblock.

创建模型

Create this Simulink model that has aMATLAB Functionblock with an unbounded variable-size array.

This image shows the model you created. A MATLAB Function block uses a constant block as an input and an Outport as an output.

  1. Create a Simulink®modelmymodel.

  2. Add aMATLAB Functionblock to the model.

  3. In theMATLAB Functionblock, add this code:

    functions = myfcn(n) Z = rand(1,n); s = sum(Z);end
  4. Add aConstantblock to the left of theMATLAB Functionblock.

  5. Add anOutportblock to the right of theMATLAB Functionblock.

  6. Connect the blocks.

Configure Model for Dynamic Memory Allocation

Make sure that you configure the model to use dynamic memory allocation for variable-size arrays inMATLAB Functionblocks. In the Configuration Parameters dialog box, in theSimulation Target > Advanced parameters类别,make sure that:

  • TheDynamic memory allocation in MATLAB functionscheck box is selected.

  • TheDynamic memory allocation threshold in MATLAB functionsparameter has the default value 65536.

Simulate Model Using Dynamic Memory Allocation

  1. Simulate the model.

  2. 在MATLABFunction Editor, to open the MATLAB®function report, clickFunction Report.

    The variables tab shows thatZis a 1-by-:? array. The colon (:) indicates that the second dimension is variable-size. The question mark (?) indicates that the second dimension is unbounded.

Simulation must use dynamic memory allocation forZbecause the second dimension ofZ没有一个上界。

Use Dynamic Memory Allocation for Bounded Arrays

When an array is unbounded, the code generator must use dynamic memory allocation. If an array is bounded, the code generator uses dynamic memory allocation only if the array size, in bytes, is greater than or equal to the dynamic memory allocation threshold. The default value for this threshold is 65536.

Dynamic memory has a run-time performance cost. By controlling its use, you can improve execution speed.

If you makeZa bounded variable-size array with a size that is greater than the threshold, the code generator uses dynamic memory allocation forZ. For example:

  1. Inmymodel, modifymyfcnso thatZhas an upper bound of 500.

    functions = myfcn(n) assert(n < 500); Z = rand(1,n); s = sum(Z);end

  2. Simulate the model.

    在MATLABfunction report, you see thatZis a 1-by-:500 array. It is variable-size with an upper bound of 500.

  3. Lower the dynamic memory allocation to a value less than or equal to4000, which is the size, in bytes, ofZ. In the Configuration Parameters dialog box, in theSimulation Target > Advanced parameters类别,set theDynamic memory allocation threshold in MATLAB functionsparameter to 4000.

  4. Simulate the model.

    The code generator uses dynamic memory allocation because the size ofZis equal to the dynamic memory allocation threshold, 4000.

Generate C Code That Uses Dynamic Memory Allocation

If you haveSimulink Coder™, you can generate C code for this model. Then, you can see how the code generator represents dynamically allocated arrays.

  1. Configure the model to use a fixed-step solver. In the Configuration Parameters dialog box, in theSolverpane, underSolver selection:

    • ForType, selectFixed-step.

    • ForSolver, selectdiscrete (no continuous states).

  2. Configure the model to create and use a code generation report. In the Configuration Parameters dialog box, in theCode Generation > Reportpane, selectCreate code generation reportandOpen report automatically.

  3. Edit the code in theMATLAB Functionblock so that it looks like this code:

    functions = myfcn(n) Z = rand(1,n); s = sum(Z);end

    Zis an unbounded variable-size array.

  4. Make sure that the model is configured for dynamic memory allocation:

    • TheDynamic memory allocation in MATLAB functionscheck box is selected.

    • TheDynamic memory allocation threshold in MATLAB functionsparameter has the default value 65536.

  5. Build the model.

  6. In the code generation report, openmymodel.c. You can tell that the code generator used dynamic memory allocation forZbecause you see theemxArraytypeemxArray_real_T_mymodel_TandemxArrayutility functions, such asmymodel_emxInit_real_T. The code generator uses anemxArraytype for variables whose memory is dynamically allocated. The generated code uses theemxArrayutility functions to manage theemxArrays.

If you have Embedded Coder®, you can customize the identifiers foremxArraytypes and the utility functions. SeeIdentifier Format Control(Embedded Coder).

Related Topics