MATLAB Answers

Remove same types of Simulink blocks using commands

10 views (last 30 days)
David K
David K on 10 Nov 2020
Commented: Andy Bartlett on 13 Nov 2020
I added multiple Simulink blocks (Dashboards,To workspace etc) in my model in order to test its behavior that I now need to remove without opening the model manually in order to have a clean model to generate C code.
Is there an existing command/script to delete same type of blocks from a system without specifing the path (using gcb/get_param/delete_block commands)?

Accepted Answer

Andy Bartlett
Andy Bartlett on 10 Nov 2020
Automatically deleting lots of blocks from a model obviously involves risk, so save a backup and proceed at your own risk.
The advice from Jonas on using approaches that don't depend on deleting blocks is good. Follow that if you can.
OK, if you must do batch delete this is how.
If you know, the "kind" of thing you want to delete your goal is straight forward using
The "kind" of block is slightly complicated.
For some Blocks, all you need to know is BlockType.
For blocks that are masked subsystems or sfunctions, you need to know the MaskType.
To help figure out the "kinds" in your model, I've attached a utility I wrote long ago to find BlockTypes and MaskTypes in a model.
This is not production quality so use at your own risk.
Here is an example call and output:
>> res = find_all_types(bdroot)
res =
11×6 table
Name Occurances ReferenceLink BlockType MaskType SFunction
_________________________ __________ ______________________________________________________ ______________________ _________________________ _______________
{'DataTypeConversion' } 23 {0×0 char } {'DataTypeConversion'} {0×0 char } {0×0 char }
{'Inport' } 18 {0×0 char } {'Inport' } {0×0 char } {0×0 char }
{'Outport' } 17 {0×0 char } {'Outport' } {0×0 char } {0×0 char }
{'SubSystem' } 9 {0×0 char } {'SubSystem' } {0×0 char } {0×0 char }
{'ModelReference' } 6 {0×0 char } {'ModelReference' } {0×0 char } {0×0 char }
{'Concatenate' } 6 {0×0 char } {'Concatenate' } {0×0 char } {0×0 char }
{'Constant' } 5 {0×0 char } {'Constant' } {0×0 char } {0×0 char }
{'Scope' } 2 {0×0 char } {'Scope' } {0×0 char } {0×0 char }
{'Data Type Propagation'} 1 {'simulink/Signal\nAttributes/Data Type\nPropagation'} {'S-Function' } {'Data Type Propagation'} {'sfix_dtprop'}
{'Switch' } 1 {0×0 char } {'Switch' } {0×0 char } {0×0 char }
{'UnitDelay' } 1 {0×0 char } {'UnitDelay' } {0×0 char } {0×0 char }
This shows about 10 BlockTypes and one MaskType used in the model I tested.
To delete all the blocks of a certain kind, run code like the following.
It tries to enforce that you saved the model before, attempting to deleting a bunch of stuff.
function dangerous_delete_all_blocks_of_type(sys,blockType,maskType)
% delete blocks of a certain type
% dangerous_delete_all_blocks_of_type(sys,blockType,maskType)
rootSys = bdroot(sys);
if strcmp('on', get_param(rootSys,'Dirty') )
error('Model is dirty. Batch deleting blocks not recommended. Save %s\n',rootSys);
if ~isempty(blockType)
propName = 'BlockType';
propValue = blockType;
propName = 'MaskType';
propValue = maskType;
% For info on options, see help find_system
blks = find_system(sys,'FollowLinks','off','LookUnderMasks','off',...
for iBlk = 1:length(blks)
curBlk = blks{iBlk};
fprintf('Deleting\n %s\n\n',curBlk)
That's the basic idea.
Andy Bartlett
Andy Bartlett on 13 Nov 2020
Hmm, when I try it DashboardScope blocks are found.
Not sure what's causing you to get different results.
>> find_all_types(bdroot)
ans =
14×6 table
Name Occurances ReferenceLink BlockType MaskType SFunction
_____________________________ __________ ______________________________________________ ___________________ _____________________________ __________
{'Gain' } 16 {0×0 char } {'Gain' } {0×0 char } {0×0 char}
{'DashboardScope' } 1 {0×0 char } {'DashboardScope' } {0×0 char } {0×0 char}
>> find_system(bdroot,'BlockType','DashboardScope')
ans =
1×1 cell array
{'f14/Dashboard Scope'}

Sign in to comment.

More Answers (1)

Jonas on 10 Nov 2020
Hi David
I don't know the answer to your specific question, but I may provide you with other options to cope with your problem.
Firstly, some blocks are automatically eliminated away before code generation, such as Dashboards, Scope blocks, terminator blocks, etc. So it may be that some blocks already are not affecting the code generation.
However, some blocks may indeed still remain in the code although you don't want to generate code for it. So secondly, I may suggest to put those blocks inside a variant subsystem. Then, use the following variant condition:
This will change the variant depending on if you are generating code with Embedded Coder, or if you are simulating (then the target file would be grt.tlc) or if you are rapid prototyping with Simulink Real-time. The variant for code generation use would be an empty block, while for the other uses you put the To Workspace blocks inside for example. Dashboards themselves don't need to be put inside the variant subsystem, but the blocks that generate the signals which are shown on the Dashboard may be put inside the variant subsystem.
With kind regards,




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!