Generate Code for MATLAB Discrete-Event System Blocks
To improve simulation performance, you can configure the MATLAB Discrete-Event
System to simulate using generated code. With the Simulate
using parameter set to Code generation
option,
the block simulates and generates code using only MATLAB® functions supported for code generation.
MATLAB Discrete-Event System blocks support code reuse for models that have multiple MATLAB Discrete-Event System blocks using the same System object™ source file. Code reuse enables the code to be generated only once for the blocks sharing the System object.
Migrate Existing MATLAB Discrete-Event System System object
Starting in R2017b, the MATLAB Discrete-Event System block can
simulate using generated code. Existing applications continue to work with the
Simulate using parameter set to Interpreted
execution
.
If you want to generate code for the block using MATLAB discrete-event system acceleration, update the System object code using these guidelines. For an example of updated MATLAB Discrete-Event System System object, see the Develop Custom Scheduler of a Multicore Control System example.
Replace Renamed matlab.DiscreteEventSystem
Methods
To take advantage of simulation with code generation for the matlab.DiscreteEventSystem
class:
In the
matlab.DiscreteEventSystem
application file, change these method names to the new names:Old Method Name New Method Name blockedImpl
blocked
destroyImpl
destroy
entryImpl
entry
exitImpl
exit
generateImpl
generate
iterateImpl
iterate
setupEventsImpl
setupEvents
timerImpl
timer
In the code, move the renamed method definitions from a protected area to a public area for each
matlab.DiscreteEventSystem
method.
Initialize System Properties
Initialize System object properties in the properties section. Do not initialize them in the constructor or other methods. In other words, you cannot use variable-size for System object properties.
Initialize Empty Arrays of Events
Use the initEventArray
to initialize arrays.
Before | After |
---|---|
function events = setupEventsImpl(obj) |
function events = setupEvents(obj) events = obj.initEventArray; |
Append Elements to Array of Structures
Append elements to array of structures. For example:
Before | After |
---|---|
events(id) = obj.eventGenerate(1, num2str(id), ... 0, obj.Priorities(id)); %#ok<*AGROW> |
events = [events obj.eventGenerate(1, int2str(id),... 0, obj.Priorities(id))]; %#ok<AGROW> |
Replace Functions That Do Not Support Code Generation
Replace functions that do not support code generation with functional equivalents that support code generation. For example:
Before | After |
---|---|
events(id) = obj.eventGenerate(1, num2str(id), ... 0, obj.Priorities(id)); %#ok<*AGROW> |
events = [events obj.eventGenerate(1, int2str(id),... 0, obj.Priorities(id))]; %#ok<AGROW> |
Declare Functions That Do Not Support Code Generation
For functions that do not support code generation and that do not have
functional equivalents, use the coder.extrinsic
function to
declare those functions as extrinsic. For example,
str2double
does not have a functional equivalent.
Before calling the coder.extrinsic
, make the returned
variable the same data type as the function you are identifying. For
example:
Before | After |
---|---|
id = str2double(tag); |
coder.extrinsic('str2double'); id = 1; id = str2double(tag); |
Do not pass System object to functions that are declared as extrinsic.
Declare only static System object methods as extrinsic.
Replace Cell Arrays
Replace cell arrays with matrices or arrays of structures.
Before | After |
---|---|
entity.data.execTime = obj.ExecTimes{id} (1); |
entity.data.execTime = obj.ExecTimes (id, 1); |
Change Flags to Logical Values
Change flags from values such as 1
and 0
to logical values, such as true
and
false
.
Manage Global Data
Manage global data while simulating with code generation using one of these:
Move Logging and Graphical Functions
Many MATLAB logging and graphical functions do not support code generation. You can move logging and graphical functions into:
A new
matlab.DiscreteEventSystem
object and configure the associated MATLAB Discrete-Event System block to simulate usingInterpreted execution
mode.An existing
simevents.SimulationObserver
object
Replace Persistent Variables
Replace persistent variable by declaring a System object property. See Create System Objects for more information.
Limitations of Code Generation with Discrete-Event System Block
Limitations include:
See Also
matlab.DiscreteEventSystem
| blocked
| cancelGenerate
| cancelIterate
| cancelTimer
| cancelForward
| entry
| eventForward
| generate
| getEntityPortsImpl
| getEntityTypesImpl
| iterate
| queueFIFO
| setupEvents
| timer
| matlab.System