删除simulink模型中除指定块外的所有块 [英] Delete all blocks except specified ones in simulink model
本文介绍了删除simulink模型中除指定块外的所有块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
clearvars-除keepVariables 之外是否有任何等效命令?可以在simulink模型中用于删除除指定的块,端口和行以外的所有块,端口和行吗?
Is there any equivalent command to the clearvars -except keepVariables which can be used in a simulink model to delete all blocks, ports and lines, except specified ones?
推荐答案
这是一种通用方法,使用内置示例vdp
进行了解释:
This is one general way to do it, explained used the in-built example vdp
:
simulink;
name = 'vdp';
%// open system, pause just for displaying purposes
open_system(name);
% pause(3)
%// find system, specify blocks to keep
allblocks = find_system(name);
ToKeep = {'Out1';'Out2'};
%// add systemname to strings
ToKeep = strcat(repmat({[name '/']},numel(ToKeep),1), ToKeep);
%// Alternative, directly, so save one line:
ToKeep = {'vdp/Out1';'vdp/Out2'};
%// create mask
ToDelete = setdiff(allblocks,ToKeep);
%// filter out main system
ToDelete = setxor(ToDelete,name);
%// try-catch inside loop as in this example not everything is deletable
for ii = 1:numel(ToDelete)
try
delete_block(ToDelete{ii})
catch
disp('Some objects couldn''t be deleted')
end
end
如果所有对象均可删除,则可以使用
If all objects are deletable you may use
cellfun(@(x) delete_block(x),ToDelete)
代替循环.
关于您的评论:
假设您只想保留所有Scope
和Out
块.您还需要通过find_system
在此处找到名称,并将其收集到列表中:
Regarding your comment:
Imagine you just want to keep all Scope
and Out
blocks. You need to find there names also by find_system
and gather them to a list:
%// what to keep
scopes = find_system(name,'BlockType','Scope')
outs = find_system(name,'BlockType','Outport')
%// gather blocks to keep
ToKeep = [scopes; outs];
%// create mask
ToDelete = setdiff(allblocks,ToKeep);
%// filter out main system
ToDelete = setxor(ToDelete,name);
这篇关于删除simulink模型中除指定块外的所有块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文