Finding Library Links using find_system
This page provides examples of the commands needed to find library links in a model. For a graphical way to investigate library links, see the Model Advisor.
Contents
- Load a suitable demo model
- Find all the "active" library links in a model
- Restricting attention to a particular library
- Find all the "disabled" library links in a model
- Restricting attention to a particular library
- Finding unresolved library links
- Use the "LinkStatus" parameter to find unresolved library links.
- Use the "SourceBlock" parameter to find unresolved library links.
- Create a parameterized library link
- Find parameterized links in a model
- Find all the libraries to which a model refers
- Tidy up
Load a suitable demo model
system = 'sldemo_hydcyl4';
load_system(system);
Find all the "active" library links in a model
This find_system command will return the names of all the blocks in the specified model which are active library links. Since we specify the FollowLinks option, this will include links within other library links.
blocks = find_system(system,'FollowLinks','on',... 'LookUnderMasks','on',... 'LinkStatus','resolved'); % Display the block names, replacing newlines with spaces for clarity for i=1:numel(blocks) disp(strrep(blocks{i},char(10),' ')); end
sldemo_hydcyl4/Pump sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 1 sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 1/Control Valve Flow sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 2 sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 2/Control Valve Flow sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 3 sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 3/Control Valve Flow sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 4 sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 4/Control Valve Flow
Restricting attention to a particular library
Another way of finding these links, which gives us the flexibility to restrict attention to particular library blocks, is to use the "ReferenceBlock" parameter of the linked block. We use a regular expression to find those which point to a particular part of the "hydlib" library.
blocks = find_system(system,'FollowLinks','on',... 'LookUnderMasks','on',... 'RegExp','on',... 'LinkStatus','resolved',... 'ReferenceBlock','hydlib/Turbulent.*'); for i=1:numel(blocks) disp(strrep(blocks{i},char(10),' ')); end
sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 1/Control Valve Flow sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 2/Control Valve Flow sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 3/Control Valve Flow sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 4/Control Valve Flow
Find all the "disabled" library links in a model
Library links which are "disabled" are not really links at all. They're just subsystems blocks with an "AncestorBlock" parameter which tells Simulink where the library block is, should you ever want to restore the link. But we can still use the "LinkStatus" parameter to find these blocks.
% Disable a library link. set_param('sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 1',... 'LinkStatus','inactive') % Now search the model and its libraries for disabled library links blocks = find_system(system,'FollowLinks','on',... 'LookUnderMasks','on',... 'LinkStatus','inactive'); for i=1:numel(blocks) disp(strrep(blocks{i},char(10),' ')); end
sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 1
Restricting attention to a particular library
Just as we did for active links, we can use the "AncestorBlock" parameter to find disabled links to particular library blocks.
blocks = find_system(system,'FollowLinks','on',... 'LookUnderMasks','on',... 'RegExp','on',... 'LinkStatus','inactive',... 'AncestorBlock','hydlib'); for i=1:numel(blocks) disp(strrep(blocks{i},char(10),' ')); end
sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 1
Finding unresolved library links
We can use similar techniques to find "unresolved" library links: those for which the library block can't be found.
% Re-enable the link we disabled earlier. set_param('sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 1',... 'LinkStatus','restore') % Make a link "unresolved" by setting its "ReferenceBlock" parameter to be % an unknown location. set_param('sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 3',... 'ReferenceBlock','missing/block'); % Force Simulink to update the "LinkStatus" parameter of the block. get_param('sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 3',... 'LinkStatus');
Warning: Unable to load block diagram 'missing'
Use the "LinkStatus" parameter to find unresolved library links.
blocks = find_system(system,'FollowLinks','on',... 'LookUnderMasks','on',... 'LinkStatus','unresolved'); for i=1:numel(blocks) disp(strrep(blocks{i},char(10),' ')); end
sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 3
Use the "SourceBlock" parameter to find unresolved library links.
The parameter which tells us where an unresolved link points is called "SourceBlock".
blocks = find_system(system,'FollowLinks','on',... 'LookUnderMasks','on',... 'RegExp','on',... 'LinkStatus','unresolved',... 'SourceBlock','.'); for i=1:numel(blocks) disp(strrep(blocks{i},char(10),' ')); end
sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 3
Create a parameterized library link
We do this by setting the value of a dialog parameter in a block within a linked subsystem. This will produce a warning telling us how to handle the effects.
set_param('sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 1/laminar flow pressure drop',... 'Gain','99');
Warning: Overriding parameters of 'sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 1/laminar flow pressure drop' which is inside a library link. These changes can be changed, propagated, or viewed using the 'Link Options' menu item
Find parameterized links in a model
Parameterized library links can be identified by their non-empty "LinkData" parameter. An added complication is that the "LinkData" for a Configurable Subsystem is attached to a block which is not itself a library link (the library link is a hidden block inside it). There is no quick find_system command to do this, because the value of the "LinkData" parameter is not a string.
% First find all active library links. linkblocks = find_system(system,'FollowLinks','on',... 'LookUnderMasks','on',... 'LinkStatus','resolved'); % Next find Configurable Subsystems, which can be identified by their % non-empty "BlockChoice" parameter. confblocks = find_system(system,'LookUnderMasks','on',... 'FollowLinks','on',... 'RegExp','on',... 'BlockType','SubSystem',... 'BlockChoice','.'); allblocks = [linkblocks(:) ; confblocks(:)]; % Now find those blocks which have a non-empty "LinkData" parameter. linkdata = get_param(allblocks,'LinkData'); hasdata = ~cellfun('isempty',linkdata); parameterized = allblocks(hasdata); for i=1:numel(parameterized) disp(strrep(parameterized{i},char(10),' ')); end % Show the link data itself fprintf('\nLinkData = \n'); disp(get_param(parameterized{1},'LinkData'));
sldemo_hydcyl4/Valve//Cylinder//Piston//Spring Assembly 1
LinkData =
BlockName: [1x26 char]
DialogParameters: [1x1 struct]
Find all the libraries to which a model refers
Having found the blocks which link to libraries, we may want to find the names of those libraries. To do this we use the ReferenceBlock parameter, and extract the library name from that. In this model, all of our links are to a single library.
blocks = find_system(system,'FollowLinks','on',... 'LookUnderMasks','on',... 'LinkStatus','resolved'); refblocks = get_param(blocks,'ReferenceBlock'); % The library name is everything before the first '/' character. unique(strtok(refblocks,'/'))
ans =
'hydlib'
Tidy up
bdclose all;
