Remove same types of Simulink blocks using commands
11 views (last 30 days)
Show older comments
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)?
0 Comments
Accepted Answer
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
find_system
delete_block
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)
open_system(sys);
rootSys = bdroot(sys);
if strcmp('on', get_param(rootSys,'Dirty') )
error('Model is dirty. Batch deleting blocks not recommended. Save %s\n',rootSys);
end
if ~isempty(blockType)
propName = 'BlockType';
propValue = blockType;
else
propName = 'MaskType';
propValue = maskType;
end
%
% For info on options, see help find_system
%
blks = find_system(sys,'FollowLinks','off','LookUnderMasks','off',...
propName,propValue);
for iBlk = 1:length(blks)
curBlk = blks{iBlk};
fprintf('Deleting\n %s\n\n',curBlk)
delete_block(curBlk);
end
end
That's the basic idea.
2 Comments
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'}
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:
strcmp('ert.tlc',get_param('controller','SystemTargetFile'))
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,
Jonas
0 Comments
See Also
Categories
Find more on Naming Conventions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!