Guy on Simulink

Simulink & Model-Based Design

Revisited: How Many Blocks are in that Model? 5

Posted by Guy Rouleau,

A long time ago, Seth wrote a post showing how to count the number of blocks in a model, including referenced models. At that time, using the functions find_mdlrefs to find all the referenced models and running sldiagnostics on each of them was the way to go.

In R2017b, Simulink Check introduces a new and easy way of finding out how many blocks are in a Simulink models, along with a lot of associated metrics: The Metrics Dashboard.

Block Count

To understand the sort of data that the Dashboard Metrics includes, I think it is a good idea to start with a simple model. For that, I will use the example model sf_car.slx. To get started, I launch the Dashboard Metrics from the Analysis menu:

Launching the Metrics Dashboard

Then I click the All Metrics button to begin the analysis:

Launching the Metrics Dashboard

Within the few seconds, I get the following report:

Launching the Metrics Dashboard

Let's click on some of those metrics to see what they can tell us. To begin, let's click on the number of blocks. This opens a table listing all the components in the model, and how many blocks they contain.

Block count

For example, we can see that the Transmission Subsystem contains 7 blocks, and 25 blocks including its descendants: Torque Converter (10 blocks) and Transmission Ratio (8 blocks).

Block count in transmission subsystem

More Metrics

In a way similar to the block count, the Metrics Dashboard can tell you how many and where in your model are the lines of MATLAB code (inside MATLAB Function blocks), lines of code inside Stateflow charts, and parameters.

More Metrics

In the above image, if I click on the number 10 in the System Interface section, I can see that the Engine subsystem uses 4 parameters from the base workspace:

Parameter Count

After verification, I confirm that the Engine subsystem really uses 4 parameters: 3 in the Engine Torque Lookup Table, and 1 in the Gain block

Parameter Count

Improving your model based on metrics

Now let's switch to a larger, more complex model. For that, I will use a hybrid electric vehicle example from the Powertrain Blockset.

One interesting metric for this model is the library reuse, which shows that many subsystems have identical copies in the model.

Library reuse

If you click on the metric to look at the details, you will notice a button saying Open Conversion Tool:

Conversion Tool

This will launch a tool that can in one click replace all the copies of the subsystem in your model by instances of one library block.


As you can imagine, doing this refactoring should help to avoid situations where you could modify one copy of the subsystem and forget to modify the other copies.

Now it's your turn

The Metrics Dashboard contains a lot more information, I recommend going through the Model Metrics documentation page to get the full list.

Try this new feature on your models and let us know in the comments below if you find something interesting. Also, let us know if there are additional metrics you are using and would like to see being included in the dashboard.

5 CommentsOldest to Newest

Scott Thomson replied on : 2 of 5

Is there an API for this? I’d like to make model complexity part of a model adviser check.
Also is this new product “Simulink Check” the same a what used to be called model adviser?

Daniel Lindner replied on : 3 of 5

Hi Scott,

The metrics infrastructure comes with a comprehensive API to collect and access metric data.
Please refer to the documentation on how to Collect Model Metrics Programmatically.
There is also an existing Model Advisor check (mathworks.metricchecks.CyclomaticComplexity) that uses the same API to report on cyclomatic complexity in the model.
I recommend using the API directly, which will provide you more details on the complexity data and more flexibility in analyzing it.

All the metric functionality is provided by the new Simulink Check product, which includes parts of the former Simulink Verification and Validation product.
Model Advisor itself comes with Simulink. Specific customization capabilities and checks, like the cyclomatic complexity check, plus other static analysis capabilities like clone detection, are provided through the Simulink Check product. More information can be found on the Simulink Check web page.


Joseph Garbrous replied on : 4 of 5

Sorry Gary but the dashboard does not work as you explained. I used the same sf_car.slx as you did. Then I select Analysis … then Dashboard … never get to All Metrics … instead I get a Hierarchy dialogue box … no matter what component I select and click OK, I never get back to the second picture you show.

Anyway to attach screenshots to a comment?

Joseph’s issue was that he does not have Simulink Check installed. The tool he had opened was the Model Advisor Dashboard, which is a different feature.

Add A Comment

Your email address will not be published. Required fields are marked *

Preview: hide