IBM Support

Creating a MATLAB 2017b Simulink model and importing it into a SimulinkBlock

Product Documentation


Abstract

This guide demonstrates how to create and configure a Mathworks MATLAB 2017b Simulink model for code generation. It demonstrates how a Simulink model can be imported into a IBM Engineering Systems Design Rhapsody 8.3.1 project so that it can be executed in the animated model.

Note that the files created in this article can be downloaded as the bottom of this page.

Also, note you can use the Configuration Set for IBM Engineering Systems Design RhapsodyIBM Engineering Systems Design Rhapsody to load in model code generation presets rather than enter them manually for each new model.  See Section 3.

Content

  1. Create the IBM Engineering Systems Design Rhapsody® project


    • Create a new SysML project and add the Mathworks® Simulink® profile for C++ or SimulinkC profile for C.
    • Create a new Block and set its stereotype to SimulinkBlock
    • Right-click the block and open Display options. In the Ports tab, select All (for Ports to Display)
       
  2. Create the Mathworks® Simulink® model


    • Open Mathworks® MATLAB® and set the directory to a suitable location
    • Right-click the Mathworks® MATLAB® browser, select "New File > Model". Name the model "ImportTest"
    • Open the Simulink® model and select the Simulink® Library browser
    • Drag the following elements to the model: a)In1 b)Gain and c)Out1
    • Arrange the elements and connect them together in the order they were added. Set the gain to 3.

      Simulink Model
       
    • Save the Simulink model
  3. Configure the Simulink model code generation.

    • Note you can use the preset Configuration Set for Rhapsody.  If you do, skip section 3 below.
    • Open the "Model Configuration Parameters" in the toolbar.
    • Select the "Solver" section.

      • Set the Solver type to "Fixed-step"
    • Select the "Code Generation" section.

      • Set the System target file field to "ert.tlc"
      • Set the language to C or C++, depending on the IBM Engineering Systems Design Rhapsody® Simulink profile selected previously in PartA.
      • Set the Generate Code Only option to True
    • Select the "Symbols" section

      • Set Global Variables to 
         
          $R$N$M

      • Set Global Types to
         
          $N$M

      • Set Fieldname of Global Types to
         
          $N$M

      • Set Subsystem Methods to
         
          $N$M$F

      • Set Subsystem Method Arguments to
         
          rt$I$N$M
      • Set Local Temporary Variables to
         
          $N$M

      • Set Local Block Output Variables to
         
          rtb_$N$M

      • Set Constant Macros to
         
          $N$M

      • Set Shared Utilities to
         
          $N$C

      • Set System-Generated Identifiers to "Classic"

        SimulinkSymbols
         
    • Select the "Interface" section

      • Set the Code Interface Packaging to "Nonreusable function"
    • Save the Simulink® model
    • Build the Simulink® model (since we specified only code generation, this will not actually build the code)
    • You should see a successful code generation report

      SimulinkCodeGenReport
       
    • Close the MATLAB® and Simulink® windows.
  4. Import the Simulink® model into the IBM Engineering Systems Design Rhapsody® project


    • Go back to the IBM Engineering Systems Design Rhapsody® project created earlier
    • Right-click the SimulinkBlock and select "Import/Sync Simulink Model"
    • In the window select:

      • The MATLAB.exe in the win64 directory
      • The Simulink® model created in step2 (ImportTest.slx)
      • The source files (ImportTest.cpp from the ImportTest_ert_rtw directory)

        ImportSimulinkModel
         
    • Press Import/Sync. Press OK on the success message once it has completed.
    • Note that the In1 and Out1 ports from the Simulink model are now present on your SimulinkBlock

      ImportedSimulinkModel
       
    • Run the Code menu > Regenerate on the project.
    • Run Code menu > Generate/Make/Run to build the project.
  5. Running the Imported SimulinkBlock

    • Add a Block called BufferIn:

      • Open the display options for the block, select show all ports on the Port tab

        ShowPorts
         
      • Add a flowport to the block of name "In1", type RhpReal and direction "Output".
      • Add a Value Property to the block called "In1" of type RhpReal, initial value: 3
      • Add a statechart to the block
    • Add a Block called BufferOut:

      • Open the display options for the block, select show all ports on the Port tab
      • Add a flowport to the block of name "Out1", type RhpReal and direction "Input".
      • Add a Value Property to the block called "Out1" of type RhpReal, initial value: 0
      • Add a statechart to the block
    • Right-click each block and select "Make an Object"
    • Connect each flowport pair using Connectors.

      FinishedRhapsodyObjectModelDiagram
       
    • Add a panel diagram

      • Add a dial control, bind it to BufferIn::In1
      • Add a digital display control, bind it to BufferIn::In1
      • Add a digital display control, bind it to BufferOut::Out1
      • Enable animation in the configuration and run Code menu > GMR again.
    • When you adjust the dial, the first display shows the original value and the second display shows the value with a gain of 3.

      RhapsodyAnimatedPanelDiagram

Downloads:

The projects created in this guide can be downloaded here:
SimulinkImport_GainExample.zip

[{"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Product":{"code":"SSB2MU","label":"IBM Engineering Systems Design Rhapsody"},"Component":"","Platform":[{"code":"PF033","label":"Windows"}],"Version":"All Versions","Edition":"","Line of Business":{"code":"LOB59","label":"Sustainability Software"}}]

Product Synonym

Rational Rhapsody

Document Information

More support for:
IBM Engineering Systems Design Rhapsody

Software version:
All Versions

Operating system(s):
Windows

Document number:
715585

Modified date:
27 May 2022

UID

ibm10715585

Manage My Notification Subscriptions