How can I access to the tabs in Simulink editor by script?

16 次查看(过去 30 天)
Usually it's possibe to open subsystems by "Open in new tab" by mouse action.
If theese tabs were not closed before saving the model it reopenes in same (all tabs open) way.
I want to create a script (e.g. for usage in pre-save callback), that closes all tabs except the first one.
The necessary command to access theese tabs was not yet found by me.
I'm using ML 2017B with matching Simulink-version.

回答(2 个)

Abhisek Pradhan
Abhisek Pradhan 2020-11-13
I am not sure if this can be done through any of the MATLAB functions but it can be done using some external automation tools, which takes control of MATLAB and can perform various GUI actions.

Alexandre de Langlade
编辑:Alexandre de Langlade 2021-11-4
I have just made a script that does exactly that !
Enjoy !
function preSave(system_name)
% preSave PreSaveFcn callback
Blocks_List = find_system(system_name);
% Since we close everything, need to open at least toplevel of
% specified system in case it was not open already
open_system(Blocks_List(1), "tab");
% Index starts at 2 to not close toplevel
for block = Blocks_List(2:end)'
blk = block{:};
% Bonus : Set Zoom factor to fit the window
if strcmp(get_param(blk, 'BlockType'), 'SubSystem')
set_param(blk, 'ZoomFactor', 'FitSystem')
end
if contains(blk,"/")
if is_stateflow(blk) % Function that checks whether system is a stateflow : look it up if you want
% sfclose considers chart names without toplevel
% name in path, so we remove it
sf_blk = extractAfter(blk,[system_name '/']);
sfclose(sf_blk);
else
close_system(blk);
end
end
end
% Finally, set Zoom factor of top level
set_param(Blocks_List{1}, 'ZoomFactor', 'FitSystem')
clear Blocks_List block blk;
end
  1 个评论
Adam Clark
Adam Clark 2022-5-9
This is great -- Thank you! I've been wanting to do this for years, but finally got around to researching the commands.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Modeling 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by