Guy on Simulink

Simulink & Model-Based Design

Subsystem Reference and Test Harness

In case you missed it, some time ago I wrote this post introducing the Subsystem Reference. I really like this feature and the way it allows you to simply store the contents of a Subsystem in a separate file.
Since its initial release in R2019b, a lot of enhancements have been made to the Subsystem Reference and today I want to highlight one I find particularly useful: Test Harnesses.

The Challenge

One recommendation I always give to users developing Simulink models is to try keeping the model in an "updatable" state as much as possible. This mostly means doing update diagram often (Shortcut: Ctrl+D). That way, if you make a mistake, you will quickly get an error describing what is wrong. Without that, if you make multiple changes before trying to update the model, the error messages might be more difficult to debug because there can be a combination of multiple inter-related issues.
A Reference Subsystem can be edited in two ways, and both have caveats regarding the above suggestion:
  • Editing standalone: Since a Subsystem file does not have a complete configuration set, it is not possible to perform an update diagram on a standalone subsystem.
  • Editing an instance in a model: In a model, it is possible to edit a Subsystem and hit Ctrl+D to validate your changes. One possible challenge with that approach is that, if the model is large and contains many blocks around the modified Subsystem, performing an update diagram might take time.
Let's see how a test harness can help with that.

Creating a Default Test Harness

Starting in MATLAB R2020b, the Subsystem tab of a Subsystem Reference file has a new Test Harness section allowing you to create test harnesses:
I am not going to describe in this post why it is always a good idea to create test harnesses for models and subsystems, you can visit the documentation page introducing test harnesses to read more on that topic.
What I want to point out in this post is that, with a test harness, we now have a context in which the subsystem can be simulated.

Update With Test Harness

Once a Test Harness has been added to a Subsystem file, the Update With Test Harness action becomes available and is associated with the same shortcut as in a model (Ctrl+D).
This means that when you edit the Subsystem standalone, you will be able to frequently hit Ctrl+D and keep the subsystem in an updatable state. Under the hood, we will accomplish the update diagram using the test harness, so if there are no errors, you don't have to explicitly open the test harness.
If there is an error, it will be reported in the Diagnostics Viewer with hyperlinks to the problematic blocks in the context of the harness.

Simulate With Test Harness

In a similar manner, we also added a "Run with Test Harness" button to the toolstrip. Using this button, the Subsystem is simulated with the default harness, and you can visualize the logged data in the Simulation Data Inspector.

Now it's your turn

Are you leveraging test harnesses with Subsystem Reference? If yes, are you taking advantage of those to ensure that your Subsystem remains "updatable" and validate simulation results in a default test harness?
Give this feature a try and let us know what you think in the comments below.
  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.