Building a Simulink Model that Contains a Model Reference for Use in VeriStand

Updated Jun 1, 2021



Mathworks Matlab®/Simulink® Software 2017b - 2020b

The process for building a model in MathWorks Simulink® Software to be used in NI VeriStand or the LabVIEW Model Interface Toolkit is described in the VeriStand Manual. However, if your Simulink model uses model references to call into another model, the process to build the model for use in VeriStand will differ from the workflow described in the VeriStand Manual. 

This article will describe how to start with a Simulink model that contains a model reference, configure the Simulink settings, and build the model into a compiled object (.dll or .so) that can be used in VeriStand.

This guide is written using VeriStand 2020 R5 and Matlab/Simulink r2020a. The steps may be different if you are using a different version of VeriStand or Simulink.

  1. Start Matlab and Simulink.
  2. Open your top-level model that contains the model reference. A model reference is usually accomplished by calling the Model block (located in the Simulink»Port & Subsystems folder of the Simulink Library Browser).
library browser.png
  1. For any blocks in the top-level model, configure the block parameters to use either global variables or variables from a Data Dictionary as the parameter value. This a requirement of how the VeriStand Model Framework exports block parameters. For each block in your top-level model that has a parameter, do the following. Here we are using the Gain block as an example:
    1. Click on the block to see its parameters in the Property Inspector.
    gain block par.png
  1. For each parameter, create a global variable or create a variable in a Data Dictionary. To create a global variable for the Gain parameter and give it a default value of 1, you could enter the following in the Matlab command window:
global GainParam
GainParam = 1

  1. Go back to the block parameters in the Property Inspector and enter the global variable you just created as the value of that parameter.
gain block par2.png
  1. Open the Simulink Coder App by going to APPS»Simulink Coder.
  2. Click on the C CODE tab.
  3. Click Settings to open the model configuration parameters.
  4. In the Code Generation category:
    1. Check the box for Generate code only. This option must be enabled in order to build models that contain model references, but it will also generate a compiled DLL or SO file during the build process.
    2. Make sure the System target file is set to the correct TLC file for your target controller. For example, here I am using the NIVeriStand_Linux_64.tlc file, which will generate the built model files for Linux RT 64-bit controllers, such as Linux RT PXI and cRIO-903x, 904x, or 905x controllers. To build the model for a different target, click the Browse button and select the corresponding target file for your target controller.

Note: for the referenced model that is called using the Model block, you must configure its settings to use the same System target file as the top-level model. However, you do not need to check the Generate code only option.
  1. Click OK to save those settings. 
  2. Save the model
  3. Click Generate Code in the top toolbar to start the build process. This will do a few things:
    • Generate the C code for the referenced model
    • Generate the C code for the top-level model
    • Build the top-level model into a compiled library (.dll or .so file)
  • Error Dot indexing is not supported for variables of this type occurs shortly after starting the build process
    • Make sure that the Generate code only option is checked in the model's Code Generation settings.
    • This error can occur if a change was made to the model configuration since the last time the model was compiled. This is due to the way that Simulink caches some model configuration settings and artifacts from the previous build. To fix this:
      1. Go to the folder containing your top-level model.
      2. Delete the contents of the slprj folder.
      3. Build the model again by clicking the Generate Code button. The model should compile now without error.
      4. If the error still occurs, save the model, and the close/reopen Matlab/Simulink.
  • Error VeriStand: The build procedure failed to compile the model: model name. This might be the result of a failed linking or compile operation of the binary file.
    • This error will occur if you have a block in the top-level model that has a parameter that is not using a global variable or variable from a Data Dictionary for its value. Please refer to step 3 in the steps above to make sure ever block parameters are using global variables or variables from a Data Dictionary. After making that change for each block, you should no longer get this error.

After the build has completed, you can check the output directory and you should have a .dll or .so file that contains the compiled model. You can import this model in a VeriStand project to deploy it to your target.

Next Steps

Refer to VeriStand 2020 R5 Manual: Adding and Configuring a Model for information on how to import your compiled model into your VeriStand project and deploy it to your target machine.