删除simulink模型中除指定块之外的所有块

时间:2015-01-22 07:24:47

标签: matlab simulink

是否有clearvars -except keepVariables的等效命令可以在simulink模型中用来删除除指定的块之外的所有块,端口和行?

1 个答案:

答案 0 :(得分:2)

这是一种的一般方法,解释使用了内置示例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

如果所有对象都是可删除的,您可以使用

cellfun(@(x) delete_block(x),ToDelete)

而不是循环。


关于你的评论: 想象一下,您只想保留所有ScopeOut块。您还需要find_system找到名称并将其收集到列表中:

%// 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);