MATLAB Answers

Rename or reorganize simulink library blocks

67 views (last 30 days)
Song on 23 Mar 2016
Answered: Gautham Ponnu on 3 Jan 2018
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.

Answers (2)

Gautham Ponnu
Gautham Ponnu on 3 Jan 2018
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',...
% Unlock Lib Link
set_param(mdl, 'LockLinksToLibrary', 'off')
%%Find lib and rename
for i=1:numel(blocks)
Hope this helps.
Cheers, Gautham

Vidya Viswanathan
Vidya Viswanathan on 30 Mar 2016
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:

Community Treasure Hunt

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

Start Hunting!