Main Content

Convert MATLAB Algorithms of Dynamic Systems into a Simulink Model

This example shows how to convert MATLAB® code that models a dynamic system using System objects into a Simulink® model. This example maps the dynamic system components defined in MATLAB onto a Simulink block diagram using blocks with similar functionality. Transforming MATLAB code to a Simulink model enhances your modeling experience by allowing you to create a block diagram of the system, simulate the model, visualize the results via graphs and animation, log data, verify and validate, and generate code.

Model Dynamic System Using MATLAB Code

Start by reviewing the MATLAB code TargetIllustrationScript that represents a dynamic system.

open('TargetIllustrationScript.m')

TargetIllustrationScript.m creates, moves, deletes, updates, and helps to visualize targets. The target creator, the target deleter, and the target viewer are defined using System objects that use internal states to store past behavior and use it in the next computational step.

  • The target database DB is a global database. The initDB function initializes the global database and creates the target struct.

  • The target creator, TargetCreator.m uses two different MATLAB namespaces TargetCreationInfoNamespace1 and TargetCreationInfoNamespace2, to create the targets. Subsequently, the target deleter, TargetDeleter.m checks and deletes any out-of-range targets.

  • The target updater updates the remaining targets using the updateAllTargetPositions function.

  • The target viewer, TargetViewer.m enables you to visualize the targets.

For this example, the target range is 5. The system deletes a target greater than 5 and updates the remaining target positions. This process loops over each time step for approximately 200 s. Run the script and observe the process.

TargetIllustrationScript;

Convert Code to Simulink Model

Open the Simulink model slexTargetIllustrationModel.slx that has similar functionality as the MATLAB code TargetIllustrationScript.m. Mapping this code onto a Simulink model further enhances the modeling experience by allowing you to create a block diagram of the system and visualize the simulation results via graphs and animation.

mdl = 'slexTargetIllustrationModel.slx';
open_system (mdl)

The Simulink model has the same functionality as the MATLAB code.

In the model:

  • A Data Store Memory block defines the target database DB. A Simulink.Bus.createObject object defines the target struct using the model post load callback function. To access the function, on the Modeling tab, choose Model Properties from Model Settings drop-down options. In the Model Properties dialog box, in the Callbacks section, select PostLoadFcn to access the function.

  • MATLAB System block TargetCreator maps the target creator onto the Simulink model. The functions in TargetCreationInfoNamespace1 and TargetCreationInfoNamespace2 are defined using scoped Simulink Function blocks and called by the TargetCreator. The TargetCreator uses getGlobalNamesImpl and getSimulinkFunctionNamesImpl methods to declare the function names.

  • MATLAB System blocks TargetDeleter and TargetViewer map TargetDeleter.m and TargetViewer.m System objects onto the Simulink model, respectively.

  • A MATLAB Function block updateTargets maps updateAllTargetPositions.m function onto the model. The number of time steps in the loop corresponds to the stop time in the toolbar.

Simulate the model and visualize the results.

out = sim(mdl);

Figure Target Trajectories contains an axes object. The axes object contains 30 objects of type animatedline. This object represents 119 118 124 128 125 130 121 123 126 116 127 129.

See Also

| | | | (Stateflow)

Related Topics