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.
Create a Simulink®model
mymodel
.Add aMATLAB Functionblock to the model.
In theMATLAB Functionblock, add this code:
functions = myfcn(n) Z = rand(1,n); s = sum(Z);end
Add aConstantblock to the left of theMATLAB Functionblock.
Add anOutportblock to the right of theMATLAB Functionblock.
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
Simulate the model.
在MATLABFunction Editor, to open the MATLAB®function report, clickFunction Report.
The variables tab shows that
Z
is 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 forZ
because 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 makeZ
a bounded variable-size array with a size that is greater than the threshold, the code generator uses dynamic memory allocation forZ
. For example:
In
mymodel
, modifymyfcn
so thatZ
has an upper bound of 500.functions = myfcn(n) assert(n < 500); Z = rand(1,n); s = sum(Z);end
Simulate the model.
在MATLABfunction report, you see that
Z
is a 1-by-:500 array. It is variable-size with an upper bound of 500.Lower the dynamic memory allocation to a value less than or equal to
4000
, 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.Simulate the model.
The code generator uses dynamic memory allocation because the size of
Z
is 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.
Configure the model to use a fixed-step solver. In the Configuration Parameters dialog box, in theSolverpane, underSolver selection:
ForType, select
Fixed-step
.ForSolver, select
discrete (no continuous states)
.
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.
Edit the code in theMATLAB Functionblock so that it looks like this code:
functions = myfcn(n) Z = rand(1,n); s = sum(Z);end
Z
is an unbounded variable-size array.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.
Build the model.
In the code generation report, open
mymodel.c
. You can tell that the code generator used dynamic memory allocation forZ
because you see theemxArray
typeemxArray_real_T_mymodel_T
andemxArray
utility functions, such asmymodel_emxInit_real_T
. The code generator uses anemxArray
type for variables whose memory is dynamically allocated. The generated code uses theemxArray
utility functions to manage theemxArray
s.
If you have Embedded Coder®, you can customize the identifiers foremxArray
types and the utility functions. SeeIdentifier Format Control(Embedded Coder).