MATLAB: Restoring library links

restore linkssimulink

Hello All, I have a instantiated library block in my model, which link is set to inactive. I have removed some parameters from its library block. Also, the instantiated block has some parameters which values are differ from the library block and i do not like to change them. How can I apply library block changes in instance without loosing local changes. It can be done via gui(Link Options->Restore Link), but I need to do this programmatically(via set_param command)
Any help is appreciated. Thanks

Best Answer

You need to be very careful about this type of operation. Depending on what do you mean by "parameters", I am not sure if you can merge the changes made both on the library blocks and the instantiated blocks in the model. But if you say that the GUI operation via Link OPtions -> Restore Link can achieve what you want, then you can do it in script. Try an example first to make sure it works before you apply to the whole model.
LibraryBlocks=find_system(MyModel,'FollowLinks','On','LookUnderMasks','All',...
'LinkStatus','inactive');
% restore libraries, from inside to outside because library might be
% nested
for i=length(LibraryBlocks):-1:1
try
Lib_Model=get_param(LibraryBlocks{i},'AncestorBlock');
Lib_Model=regexprep(Lib_Model,'/.+','');
open_system(Lib_Model);
set_param(LibraryBlocks{i},'LinkStatus','restore');
fprintf(1, 'Info: Restore block ''%s'' from library file "%s".\n\n',LibraryBlocks{i},Lib_Model);
catch %#ok<*CTCH>
fprintf(2, 'ERROR: Can not restore block ''%s'' to library file "%s".\n\n',LibraryBlocks{i},Lib_Model);
end
end