{"id":170,"date":"2011-12-05T20:00:39","date_gmt":"2011-12-05T20:00:39","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=170"},"modified":"2011-12-06T08:49:22","modified_gmt":"2011-12-06T13:49:22","slug":"initializing-buses-using-a-matlab-structure","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2011\/12\/05\/initializing-buses-using-a-matlab-structure\/","title":{"rendered":"Initializing buses using a MATLAB Structure"},"content":{"rendered":"<p>Did you notice that since R2010a it is possible to initialize buses with a MATLAB structure? If you were not aware, here is how it works.<\/p>\r\n\r\n<p><strong>Starting from a Structure<\/strong><\/p>\r\n\r\n<p>Let's say I have a structure in the MATLAB workspace and I would like to make that a bus signal in Simulink. I can use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/simulink\/slref\/simulink.bus.createobject.html\">Simulink.Bus.createObject<\/a> to create a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/simulink\/slref\/simulink.bus.html\">bus object<\/a>:<\/p>\r\n\r\n<code>\r\nmyStruct.a = 5;<br>\r\nmyStruct.b = true;<br>\r\nmyStruct.c.x1 = 22;<br>\r\nmyStruct.c.x2 = 3;<br>\r\nbusInfo = Simulink.Bus.createObject(myStruct);<br>\r\n<\/code>\r\n\r\n<p>Then I can configure a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/simulink\/slref\/constant.html\">Constant<\/a> block to use this bus object as data type and to use <tt>myStruct<\/tt> as value.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2011Q4\/constantDialog.png\" alt=\"Configuring a Constant block to output a bus\" \/><\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2011Q4\/busFromConstant.png\" alt=\"Constant block outputing a bus signal\" \/><\/p>\r\n\r\n<p><strong>Starting from a model<\/strong><\/p>\r\n\r\n<p>Let's say I have the following model where a bus signal is going through a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/simulink\/slref\/unitdelay.html\">Unit Delay<\/a> block. I want to specify different initial values for the elements of the bus.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2011Q4\/busDelay.png\" alt=\"Specifying the initial condition for a bus signal\" \/><\/p>\r\n\r\n<p>In that case I can use the handle to a port of the Unit Delay block as input to <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/simulink\/slref\/simulink.bus.creatematlabstruct.html\">Simulink.Bus.createMATLABStruct<\/a>. This will create a MATLAB structure with the same hierarchy as the bus, filled with zeros. I can then overwrite fields of this structure as needed and use it as the initial value of the Unit Delay.<\/p>\r\n\r\n<code>\r\nportHandle = get_param(gcb,'PortHandles');<br>\r\ninitStruct = Simulink.Bus.createMATLABStruct(portHandle.Inport);<br>\r\ninitStruct.bus2.Pulse = 5;<br>\r\ninitStruct.signal1.Chirp = 3;<br>\r\n<\/code>\r\n\r\n<p><strong>Requirements to Use Bus Signal Initialization<\/strong><\/p>\r\n\r\n<p>To enable bus signal initialization, you need to set two Configuration Parameter diagnostics:\r\n<ul>\r\n\t<li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/simulink\/gui\/bq8t6s8.html#bq8t67c-1\">Mux blocks used to create bus signals<\/a> to <tt>error<\/tt>.<\/li>\r\n\t<li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/simulink\/gui\/bq8t6qh.html#brphwdw-1\">Underspecified initialization detection<\/a> to <tt>simplified<\/tt>.<\/li>\r\n<\/ul>\r\nEven if you don't need to initialize buses, I always recommend using those setting. They help making your models easier to understand and improve the consistency of simulation results.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Look at <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/simulink\/ug\/bsff8zh.html\">Specifying Initial Conditions for Bus Signals<\/a> for more details and let us know how you will use this feature by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=170&amp;#comment\">comment here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Did you notice that since R2010a it is possible to initialize buses with a MATLAB structure? If you were not aware, here is how it works.\r\n\r\nStarting from a Structure\r\n\r\nLet's say I have a structure... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2011\/12\/05\/initializing-buses-using-a-matlab-structure\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,33],"tags":[236,162],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/170"}],"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\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/170\/revisions"}],"predecessor-version":[{"id":184,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/170\/revisions\/184"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}