{"id":10640,"date":"2019-04-05T09:00:56","date_gmt":"2019-04-05T13:00:56","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=10640"},"modified":"2019-07-31T11:34:08","modified_gmt":"2019-07-31T15:34:08","slug":"do-you-find-yourself-searching-for-connection-in-simulink","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2019\/04\/05\/do-you-find-yourself-searching-for-connection-in-simulink\/","title":{"rendered":"Do You Find Yourself Searching for Connection (in Simulink)?"},"content":{"rendered":"<p><html xmlns:fo=\"http:\/\/www.w3.org\/1999\/XSL\/Format\" xmlns:xlink=\"http:\/\/www.w3.org\/1999\/xlink\"><\/p>\n<p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/603712-greg-wolff\">Greg&#8217;s<\/a> pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/69797-slquery\">slQuery<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/11581858-robert-rasche\">Robert Rasche<\/a>. <\/p>\n<p>Robert has created an impressive mechanism for finding and operating on elements of Simulink models from MATLAB scripts and functions. <\/p>\n<p>The part I feel he understates in his description of the entry is the ability to find blocks based on what the blocks are connected to, not just properties of the blocks themselves. <\/p>\n<p>Once you get a handle on the query syntax, it&#8217;s pretty trivial to create complex queries of Simulink model blocks.<\/p>\n<h3>Find Blocks with Specific Properties<\/h3>\n<p>If you want to access blocks in a Simulink model, the standard APIs do enable you to locate blocks based on the properties of those blocks. <\/p>\n<p>The standard API most users know about is the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/simulink\/slref\/find_system.html\"> <inline style=\"font-family: monospace, monospace; font-size: inherit;\">find_system<\/inline><\/a> function. A more recent addition is the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/simulink\/slref\/simulink.findblocks.html\"> <inline style=\"font-family: monospace, monospace; font-size: inherit;\">Simulink.findBlocks<\/inline><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/simulink\/slref\/simulink.findblocksoftype.html\"> <inline style=\"font-family: monospace, monospace; font-size: inherit;\">Simulink.findBlocksOfType<\/inline><\/a> functions which provide the same functionality as <inline style=\"font-family: monospace, monospace; font-size: inherit;\">find_system <\/inline>and are more robust (for example: making sure to locate <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/what-is-a-variant.html\">inactive variants<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/what-is-a-variant.html#bvnsme2\">commented blocks<\/a> as well) <\/p>\n<p>If you want to search for a block based on property values, the standard API and <inline style=\"font-family: monospace, monospace; font-size: inherit;\">slQuery<\/inline> offer the same capabilities, though through two different approaches. <\/p>\n<p>Find All Atomic Subsystems with Whitespace in the Name:<\/p>\n<p><b> <inline style=\"font-family: monospace, monospace; font-size: inherit;\">slQuery<\/inline><\/b><\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">asbhl20_VehicleSystemsModel;\r\nblocks = slQuery(<span style=\"color:rgb(160, 32, 240);\">'SubSystem[Name~=\\w+\\s+\\w+, TreatAsAtomicUnit=on]'<\/span>);\r\n<span style=\"color:rgb(34, 139, 34);\">% Replace whitespace in block names with underscore \"_\"<\/span>\r\nnames = regexprep(strtrim(blocks.Name), <span style=\"color:rgb(160, 32, 240);\">'\\s+'<\/span>, <span style=\"color:rgb(160, 32, 240);\">'_'<\/span>)';\r\nnames(1:7)\r\n<\/pre>\n<pre class=\"output\" style=\"font-family:monospace;border:none;background-color:white;color:rgba(64, 64, 64, 1);\">ans = <em>7&times;1 cell array<\/em>\r\n    {'Air_Data_Computer'                }\r\n    {'Log_LL1_Actuator_Position_Sensors'}\r\n    {'Log_LL2_Actuator_Position_Sensors'}\r\n    {'Log_LU1_Actuator_Position_Sensors'}\r\n    {'Log_LU2_Actuator_Position_Sensors'}\r\n    {'Log_Hydraulic_System_1_Sensors'   }\r\n    {'Log_Hydraulic_System_2_Sensors'   }\r\n\r\n<\/pre>\n<p><b> <inline style=\"font-family: monospace, monospace; font-size: inherit;\">Simulink.findBlocksOfType<\/inline><\/b><\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">blocks = Simulink.findBlocksOfType(bdroot, <span style=\"color:rgb(160, 32, 240);\">'SubSystem'<\/span>, <span style=\"color:rgb(0, 0, 255);\">...<\/span>\r\n            <span style=\"color:rgb(160, 32, 240);\">'Name'<\/span>, <span style=\"color:rgb(160, 32, 240);\">'\\w+\\s+\\w+'<\/span>, <span style=\"color:rgb(0, 0, 255);\">...<\/span>\r\n            <span style=\"color:rgb(160, 32, 240);\">'TreatAsAtomicUnit'<\/span>, <span style=\"color:rgb(160, 32, 240);\">'on'<\/span>, <span style=\"color:rgb(0, 0, 255);\">...<\/span>\r\n            Simulink.FindOptions(<span style=\"color:rgb(160, 32, 240);\">'RegExp'<\/span>, true));\r\n<span style=\"color:rgb(34, 139, 34);\">% Replace whitespace in block names with underscore \"_\"<\/span>\r\nnames = regexprep(strtrim(get(blocks, <span style=\"color:rgb(160, 32, 240);\">'Name'<\/span>)), <span style=\"color:rgb(160, 32, 240);\">'\\s+'<\/span>, <span style=\"color:rgb(160, 32, 240);\">'_'<\/span>);\r\nnames(1:7)\r\n<\/pre>\n<pre class=\"output\" style=\"font-family:monospace;border:none;background-color:white;color:rgba(64, 64, 64, 1);\">ans = <em>7&times;1 cell array<\/em>\r\n    {'Air_Data_Computer'                }\r\n    {'Log_LL1_Actuator_Position_Sensors'}\r\n    {'Log_LL2_Actuator_Position_Sensors'}\r\n    {'Log_LU1_Actuator_Position_Sensors'}\r\n    {'Log_LU2_Actuator_Position_Sensors'}\r\n    {'Log_Hydraulic_System_1_Sensors'   }\r\n    {'Log_Hydraulic_System_2_Sensors'   }\r\n\r\n<\/pre>\n<p>The standard API uses the addition of functional arguements to build a query, while <inline style=\"font-family: monospace, monospace; font-size: inherit;\">slQuery<\/inline> uses a custom query syntax. <\/p>\n<h3>Find Blocks with Specific Connections<\/h3>\n<p>The slQuery function offers a way to search for blocks based on their connections to other elements in the Simulink model. This pretty much blows my mind! <\/p>\n<p>In my 20 years of using Simulink, I could have definitely used features like this on several occasions.<\/p>\n<p>This is something that the standard API functions <inline style=\"font-family: monospace, monospace; font-size: inherit;\">find_system<\/inline> and <inline style=\"font-family: monospace, monospace; font-size: inherit;\">Simulink.findBlocks <\/inline>does not offer. <\/p>\n<p><strong>Find Inports directly connected to a Gain block<\/strong><\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">asbhl20;\r\nr = slQuery(<span style=\"color:rgb(160, 32, 240);\">'Inport -&gt; Gain'<\/span>);\r\ncell2table([r(1).Parent.Name; r(1).Name; r(2).Name], <span style=\"color:rgb(0, 0, 255);\">...<\/span>\r\n           <span style=\"color:rgb(160, 32, 240);\">'RowNames'<\/span>, {<span style=\"color:rgb(160, 32, 240);\">'Parent'<\/span>, <span style=\"color:rgb(160, 32, 240);\">'Inport'<\/span>, <span style=\"color:rgb(160, 32, 240);\">'Gain'<\/span>})\r\n<\/pre>\n<pre class=\"output\" style=\"font-family:monospace;border:none;background-color:white;color:rgba(64, 64, 64, 1);\">ans=<em>3&times;4 table<\/em>\r\n                         Var1                Var2      Var3           Var4       \r\n              __________________________    ______    ______    _________________\r\n\r\n    Parent    'Low Altitude&#8629;Intensity'     'Hqgw'    'Hrgw'    'cascvt'         \r\n    Inport    'Windspeed &#8629;at 20ft (6m)'    'V'       'V'       'In1'            \r\n    Gain      'sigma_wg '                   'pi\/4'    'pi\/3'    'Unit Conversion'\r\n\r\n<\/pre>\n<p>Note that this searches currently open Simulink model by default. <\/p>\n<p><strong>Find Feedback Loops<\/strong><\/p>\n<p>There are more sophisticated connection searches available, including the ability to look for feedback loops.<\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">vdp;\r\nr = slQuery(<span style=\"color:rgb(160, 32, 240);\">'#vdp \/ Integrator &gt;&gt; $1'<\/span>) ;\r\nr.Name\r\n<\/pre>\n<pre class=\"output\" style=\"font-family:monospace;border:none;background-color:white;color:rgba(64, 64, 64, 1);\">ans = <em>2&times;2 cell array<\/em>\r\n    {'vdp'}    {'vdp'}\r\n    {'x1' }    {'x2' }\r\n\r\n<\/pre>\n<p>This syntax looks for a block that is connected to itself.<\/p>\n<h3>Why Did I Choose This Entry?<\/h3>\n<p>Firstly, I thought this was a really creative way to attack the problems Robert was facing. He leverages the extensibility of the MATLAB Language to great effect. <\/p>\n<p>In addition, this File Exchange entry is <a href=\"https:\/\/github.com\/arinar\/slQuery\/blob\/master\/README.md\">documented<\/a>, and provides a number of <a href=\"https:\/\/github.com\/arinar\/slQuery\/blob\/master\/test\/slQuery_test.m\">test cases<\/a>; two elements that make it easier to understand how to use the <inline style=\"font-family: monospace, monospace; font-size: inherit;\">slQuery<\/inline> features and appeal to my nature. <\/p>\n<h3>What Are Your Impressions of This Capability?<\/h3>\n<p>Let us know <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=10640#respond\">here<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\nGreg&#8217;s pick this week is slQuery by Robert Rasche.<br \/>\nRobert has created an impressive mechanism for finding and operating on elements of Simulink models from MATLAB scripts and functions.<br \/>\nThe&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2019\/04\/05\/do-you-find-yourself-searching-for-connection-in-simulink\/\">read more >><\/a><\/p>\n","protected":false},"author":36,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10640"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=10640"}],"version-history":[{"count":9,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10640\/revisions"}],"predecessor-version":[{"id":10888,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10640\/revisions\/10888"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=10640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=10640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=10640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}