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.
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:
Then I click the All Metrics button to begin the analysis:
Within the few seconds, I get the following report:
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.
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).
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.
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:
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
Improving your model based on metrics
One interesting metric for this model is the library reuse, which shows that many subsystems have identical copies in the model.
If you click on the metric to look at the details, you will notice a button saying Open 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.
3 CommentsOldest to Newest
Cool feature, I got just over 102.000 blocks.
Downloaded and installed 2017b today just to try this.
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?
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.