Main Content

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:

  1. In the matlab.DiscreteEventSystem application file, change these method names to the new names:

    Old Method NameNew Method Name
    blockedImplblocked
    destroyImpldestroy
    entryImplentry
    exitImplexit
    generateImplgenerate
    iterateImpliterate
    setupEventsImplsetupEvents
    timerImpltimer
  2. 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.

BeforeAfter
 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:

BeforeAfter
 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:

BeforeAfter
 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:

BeforeAfter
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.

BeforeAfter
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 using Interpreted 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

| | | | | | | | | | | | | | |

Related Topics