Rename or reorganize simulink library blocks

49 ビュー (過去 30 日間)
Song
Song 2016 年 3 月 23 日
回答済み: Gautham Ponnu 2018 年 1 月 3 日
Hello, I want to ask a question about the renaming or reorganize blocks in self-made Simulink library.
It seems that every time I change the name of the block in library, all links of that block in the normal Simulink model are gone. I think lots of people have faced this problem before. Does anyone have an idea or a script/function which can do this? e.g. right click on the the library block, insert a new name, then all its linked blocks in the current opened Simulink models are renamed/updated.
Also about it interests me whether there is a way to reorganize the blocks in library into a sub-folder, but at the same time the blocks' old links remain still working.
Thanks
Song

回答 (2 件)

Gautham Ponnu
Gautham Ponnu 2018 年 1 月 3 日
Hello,
I made a small script to do this.
So, basically if you have a 'Block_A' in your library and you want to change it.
You copy 'Block_A' and rename it to 'Block_B' and make any changes you want except modifying the parameters.
Now, you call this script as renameLibLinks('modelname','Block_A','Block_B'). This script will parse through the entire model hierarchy ( including any model references) and replace all links to Block_A with Block_B. Once this is complete you can then delete Block_A in your library.
function renameLibLinks(mdl,origLibBlockName,newLibBlockName)
% RENAMELIBLINKS(model, orignalLibraryBlockName, newLibraryBlockName)
% This function will parse through the model for all blocks which are
% linked to originalLibraryBlockName and replace their link with
% newLibraryBLockName
%
% eg. >> renameLibLinks(gcs,'Control/block_A','Coordinates/block_B')
%%Load Model
system = load_system(mdl);
origRefBlock = ['mbdriLib/',origLibBlockName,'.*'];
newRefBlock = ['mbdriLib/',newLibBlockName];
blocks = find_system(system,'FollowLinks','on',...
'LookUnderMasks','on',...
'RegExp','on',...
'LinkStatus','resolved',...
'ReferenceBlock',origRefBlock);
% Unlock Lib Link
set_param(mdl, 'LockLinksToLibrary', 'off')
%%Find lib and rename
for i=1:numel(blocks)
set_param(blocks(i),'ReferenceBlock',newRefBlock);
end
end
Hope this helps.
Cheers, Gautham

Vidya Viswanathan
Vidya Viswanathan 2016 年 3 月 30 日
Hi Song,
I understand that you are looking for an automated way of renaming certain blocks in all the Simulink models that are currently open by accessing the block in Simulink Library Browser. I am not sure if there is a method to do that directly. However, in regard to your second question on creating a custom subfolder in Simulink Library Browser, there is indeed a method to accomplish the same. You can add custom libraries (which can contain the default library blocks as well) in the library browser. Refer to the following link for an illustration:
Regards,
Vidya

カテゴリ

Help Center および File ExchangeProgrammatic Model Editing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by