{"id":5,"date":"2008-03-04T22:45:10","date_gmt":"2008-03-05T03:45:10","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2008\/03\/04\/searching-in-simulink\/"},"modified":"2008-03-28T00:26:13","modified_gmt":"2008-03-28T05:26:13","slug":"searching-in-simulink","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2008\/03\/04\/searching-in-simulink\/","title":{"rendered":"Searching in Simulink"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Here we have an <a href=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q1\/pidLibrary.mdl\">example library<\/a> consisting of masked subsystems.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">sys = <span style=\"color: #A020F0\">'pidLibrary'<\/span>;\r\nopen_system(sys)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q1\/searching_in_simulink_01.png\"> <p>When you double click on a block, you get the mask dialog, and the <b>Help<\/b> button displays the <tt>MaskHelp<\/tt> if you click on it.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q1\/PIDMaskHelp.png\"> <\/p>\r\n   <p>Some of these blocks are properly documented, but others have no <tt>MaskHelp<\/tt>.  You could click through all the blocks in this library and click on the help button, but that would be a painful manual\r\n      process. How can you audit this library and ensure that the components are properly documented?\r\n   <\/p>\r\n   <p><b>find_system<\/b><\/p>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/find_system.html\"><tt>find_system<\/tt><\/a> is a very powerful tool for scripting in Simulink which allows you to automate tasks that might otherwise be time-consuming\r\n      to perform.  The <tt>find_system<\/tt> syntax is very specific:\r\n   <\/p><pre>find_system(sys, 'c1', cv1, 'c2', cv2,...'p1', v1, 'p2', v2,...)<\/pre><p><tt>find_system<\/tt> takes a set of constraints (<i><tt>'c1', cv1, 'c2', cv2<\/tt><\/i>)  and searches for blocks in the system (<i><tt>sys<\/tt><\/i>) that have the specified parameter values (<i><tt>'p1', v1, 'p2', v2<\/tt><\/i>).  Notice that all constraint\/value pairs are passed to <tt>find_system<\/tt> <i>before<\/i> the parameter\/value pairs. This confused me once when I got the order wrong, so watch out for that.\r\n   <\/p>\r\n   <p>Here is a quick <tt>find_system<\/tt> command that returns the right answer.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">blksNoHelp = find_system(sys,<span style=\"color: #A020F0\">'MaskHelp'<\/span>,<span style=\"color: #A020F0\">''<\/span>)\r\nhilite_system(blksNoHelp,<span style=\"color: #A020F0\">'find'<\/span>)<\/pre><pre style=\"font-style:oblique\">blksNoHelp = \r\n    'pidLibrary\/Continuous PI \r\nController'\r\n    'pidLibrary\/Discrete PI \r\nController'\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q1\/searching_in_simulink_02.png\"> <p>It is important to be aware of the search constraint defaults.<\/p>\r\n   <div>\r\n      <ul>\r\n         <li><tt>SearchDepth<\/tt> is set to all levels, but this can be changed to 0 for only open systems, 1 to search only the specified system, 2 for that\r\n            system and its children, etc.\r\n         <\/li>\r\n         <li><tt>LookUnderMasks<\/tt> defaults to <tt>graphical<\/tt> only, which means <tt>find_system<\/tt> doesn&#8217;t look under functional masks (those with a dialog and workspace).\r\n         <\/li>\r\n         <li><tt>FollowLinks<\/tt> defaults to <tt>off<\/tt>, which means linked blocks are not searched.  This is very important when the block might reside in a library linked from\r\n            your system.\r\n         <\/li>\r\n         <li><tt>FindAll<\/tt> defaults to <tt>off<\/tt>, which means you will only get blocks back from the search.  The other option is to specify <tt>on<\/tt> and <tt>find_system<\/tt> will also return lines and annotations from the model.\r\n         <\/li>\r\n         <li><tt>CaseSensitive<\/tt> is <tt>on<\/tt> by default, so make sure your caps lock isn&#8217;t turned on.  This constraint affects the value inputs only; parameters are not\r\n            treated as being case sensitive.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>In our example, if we were also concerned with the contents of those masked blocks, we would need to expand our search to\r\n      <tt>LookUnderMasks<\/tt>. This search would then return Simulink base blocks because they do not use <tt>MaskHelp<\/tt>.  To reduce the results to only those blocks that are masked, we can do a search where the <tt>Mask<\/tt> property is set to <tt>on<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">blksNoHelp2 = find_system(sys,<span style=\"color: #A020F0\">'LookUnderMasks'<\/span>,<span style=\"color: #A020F0\">'all'<\/span>,<span style=\"color: #0000FF\">...<\/span>\r\n                             <span style=\"color: #A020F0\">'Mask'<\/span>,<span style=\"color: #A020F0\">'on'<\/span>,<span style=\"color: #A020F0\">'MaskHelp'<\/span>,<span style=\"color: #A020F0\">''<\/span>)\r\nhilite_system(setdiff(blksNoHelp2,blksNoHelp),<span style=\"color: #A020F0\">'find'<\/span>)<\/pre><pre style=\"font-style:oblique\">blksNoHelp2 = \r\n    'pidLibrary\/Continuous PI \r\nController'\r\n    'pidLibrary\/Discrete PI \r\nController'\r\n    'pidLibrary\/Discrete PID \r\nController\/Discrete Derivative'\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q1\/searching_in_simulink_03.png\"> <p>Another reason constraints are so important is that the amount of work done by <tt>find_system<\/tt> is proportional to the number of blocks included in the search.  If you are looking under all masks, following all links\r\n      and searching the full depth of the model hierarchy, the speed of the search will be proportional to the size of your model.\r\n       <tt>find_system<\/tt> is not a Simulink Google search that skips across indexed model information. When <tt>find_system<\/tt> is called it has to resolve all block parameters as it works. This means that you will evaluate all the dialog code and parameters\r\n      required by the search. To speed up <tt>find_system<\/tt>, include the appropriate constraints to reduce the number of blocks being evaluated.\r\n   <\/p>\r\n   <p><b>Tip:<\/b> Where ever possible include a <tt>SearchDepth<\/tt> to cast a small net.\r\n   <\/p>\r\n   <p><b>Challenge<\/b><\/p>\r\n   <p>This got me thinking about how you might perform the inverse of this search. How would you find blocks that have <tt>MaskHelp<\/tt> without specifying the value it is set to?  The first person to provide a correct answer in the comments gains fame and notoriety\r\n      as a <tt>find_system<\/tt> expert.\r\n   <\/p>\r\n   <p>Did you already know how to use <tt>find_system<\/tt>?  This is my second post and I&#8217;m looking for feedback on how much experience you have with Simulink. Post a comment and tell\r\n      me what you think.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_5462c373b5ba450e81161c26ee35cba8() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='5462c373b5ba450e81161c26ee35cba8 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5462c373b5ba450e81161c26ee35cba8';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Seth Popinchalk';\r\n        copyright = 'Copyright 2008 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_5462c373b5ba450e81161c26ee35cba8()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.6<br><\/p>\r\n<\/div>\r\n<!--\r\n5462c373b5ba450e81161c26ee35cba8 ##### SOURCE BEGIN #####\r\n%% Searching in Simulink\r\n%\r\n% Here we have an <https:\/\/blogs.mathworks.com\/images\/seth\/2008Q1\/pidLibrary.mdl example library> \r\n% consisting of masked subsystems. \r\n\r\nsys = 'pidLibrary';\r\nopen_system(sys)\r\n\r\n%%\r\n% When you double click on a block, you get the mask dialog, and the *Help*\r\n% button displays the |MaskHelp| if you click on it.\r\n%%\r\n% <<PIDMaskHelp.png>>\r\n\r\n%%\r\n% Some of these blocks are properly documented, but others have no\r\n% |MaskHelp|.  You could click through all the blocks in this library and\r\n% click on the help button, but that would be a painful manual process. How\r\n% can you audit this library and ensure that the components are properly\r\n% documented?   \r\n\r\n%%\r\n% *find_system*\r\n%\r\n% <https:\/\/www.mathworks.com\/help\/simulink\/slref\/find_system.html |find_system|>\r\n% is a very powerful tool for scripting in Simulink which allows you to\r\n% automate tasks that might otherwise be time-consuming to perform.  The\r\n% |find_system| syntax is very specific:\r\n%\r\n%  find_system(sys, 'c1', cv1, 'c2', cv2,...'p1', v1, 'p2', v2,...)\r\n%%\r\n% |find_system| takes a set of constraints (_|'c1', cv1, 'c2', cv2|_)  and\r\n% searches for blocks in the system (_|sys|_) that have the specified\r\n% parameter values (_|'p1', v1, 'p2', v2|_).  Notice that all\r\n% constraint\/value pairs are passed to |find_system| _before_ the\r\n% parameter\/value pairs. This confused me once when I got the order wrong,\r\n% so watch out for that. \r\n\r\n%%\r\n% Here is a quick |find_system| command that returns the right answer. \r\n%%\r\nclose_system(sys)\r\n%%\r\nopen_system(sys)\r\nblksNoHelp = find_system(sys,'MaskHelp','')\r\nhilite_system(blksNoHelp,'find')\r\n\r\n%%\r\n% It is important to be aware of the search constraint defaults.\r\n% \r\n% * |SearchDepth| is set to all levels, but this can be changed to 0 for only open systems, 1 to search only the specified system, 2 for that system and its children, etc.\r\n% * |LookUnderMasks| defaults to |graphical| only, which means |find_system| doesn\u00e2\u20ac\u2122t look under functional masks (those with a dialog and workspace).\r\n% * |FollowLinks| defaults to |off|, which means linked blocks are not searched.  This is very important when the block might reside in a library linked from your system.\r\n% * |FindAll| defaults to |off|, which means you will only get blocks back from the search.  The other option is to specify |on| and |find_system| will also return lines and annotations from the model.\r\n% * |CaseSensitive| is |on| by default, so make sure your caps lock isn\u00e2\u20ac\u2122t turned on.  This constraint affects the value inputs only; parameters are not treated as being case sensitive.\r\n%\r\n%%\r\n% In our example, if we were also concerned with the contents of those\r\n% masked blocks, we would need to expand our search to |LookUnderMasks|.\r\n% This search would then return Simulink base blocks because they do not\r\n% use |MaskHelp|.  To reduce the results to only those blocks that are\r\n% masked, we can do a search where the |Mask| property is set to |on|. \r\n%\r\nblksNoHelp2 = find_system(sys,'LookUnderMasks','all',...\r\n                             'Mask','on','MaskHelp','')\r\nhilite_system(setdiff(blksNoHelp2,blksNoHelp),'find')\r\n\r\n%%\r\n% Another reason constraints are so important is that the amount of work\r\n% done by |find_system| is proportional to the number of blocks included in\r\n% the search.  If you are looking under all masks, following all links and\r\n% searching the full depth of the model hierarchy, the speed of the search\r\n% will be proportional to the size of your model.  |find_system| is not a\r\n% Simulink Google search that skips across indexed model information. When\r\n% |find_system| is called it has to resolve all block parameters as it\r\n% works. This means that you will evaluate all the dialog code and\r\n% parameters required by the search. To speed up |find_system|, include the\r\n% appropriate constraints to reduce the number of blocks being evaluated.\r\n%\r\n% *Tip:* Where ever possible include a |SearchDepth| to cast a small net.\r\n\r\n%%\r\n% *Challenge*\r\n%\r\n% This got me thinking about how you might perform the inverse of this\r\n% search. How would you find blocks that have |MaskHelp| without specifying\r\n% the value it is set to?  The first person to provide a correct answer in\r\n% the comments gains fame and notoriety as a |find_system| expert.\r\n%\r\n% Did you already know how to use |find_system|?  This is my second post\r\n% and I\u00e2\u20ac\u2122m looking for feedback on how much experience you have with\r\n% Simulink. Post a comment and tell me what you think.\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n##### SOURCE END ##### 5462c373b5ba450e81161c26ee35cba8\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Here we have an example library consisting of masked subsystems.\r\n   sys = 'pidLibrary';\r\nopen_system(sys) When you double click on a block, you get the mask dialog, and the Help button displays... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2008\/03\/04\/searching-in-simulink\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=5"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=5"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=5"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=5"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}