{"id":142,"date":"2011-06-07T12:57:01","date_gmt":"2011-06-07T12:57:01","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2011\/06\/07\/data-store-enhancement-accessing-specific-bus-and-array-elements\/"},"modified":"2011-06-07T13:02:01","modified_gmt":"2011-06-07T13:02:01","slug":"data-store-enhancement-accessing-specific-bus-and-array-elements","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2011\/06\/07\/data-store-enhancement-accessing-specific-bus-and-array-elements\/","title":{"rendered":"Data Store Enhancement: Accessing Specific Bus and Array Elements"},"content":{"rendered":"<p>In Simulink R2011a, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/ug\/f4-141977.html\">Data Store<\/a> blocks have been improved to <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/ug\/bra7wyn.html#bsvjg67\">support bus signals<\/a> and more.  To give you a quick idea, these enhancements can transform the syntax you are used to on the left to what you see on the right!  This new syntax simplifies the model and the generated code.  In this post I will show you some of the new capabilities that make this possible.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2011Q2\/newSyntax.png\" alt=\"Advantages of the specific elements assignment feature of the Data Store in R2011a\"><\/p>\r\n\r\n\r\n<strong><p>Bus Support<\/p><\/strong>\r\n\r\n<p>Data Stores can now be used with buses!  To connect a nonvirtual bus signal to a data store, all you have to do is specify the name of the bus object in the Data Type field of the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/slref\/datastorememory.html\">Data Store Memory<\/a> block:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2011Q2\/DSM_dialog.png\" alt=\"specifying a bus object for a Data Store Memory block\"><\/p>\r\n\r\n<p>Optionally, you can also specify a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/ug\/bsff8zh.html#bsff871\">MATLAB structure as the bus initial value<\/a>. I recommend using the function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/slref\/simulink.bus.creatematlabstruct.html\">Simulink.Bus.createMATLABStruct<\/a> or the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/ug\/brin2jr-1.html\">Bus Editor<\/a> to generate the MATLAB structure from a bus object.<\/p>\r\n\r\n\r\n<strong><p>Accessing specific elements of a bus or a array<\/p><\/strong>\r\n\r\n<p>Before R2011a, Data Store Write and Data Store Read blocks had only one port. With this port, you could write or read  the entire memory region.<\/p>\r\n\r\n<p>While implementing bus support, we added the option to write or read specific elements of a bus or an array. To allow this, a new <em>Element Assignment<\/em> tab has been added to the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/slref\/datastoreread.html\">Data Store Read<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/slref\/datastorewrite.html\">Data Store Write<\/a> blocks. Using this tab, you can select the components of the bus you want to write or read.<\/p>\r\n\r\n<p>Let's look at an example. Once I have created bus object and configured the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/slref\/datastorememory.html\">Data Store Memory<\/a> block to use it, I open the dialog of a Data Store Read or Write block and select the <em>Element Assignment<\/em> tab. For this example, I decide to write the second column of element <em>A.b.b1<\/em> and the 4th element of <em>A.b.b2<\/em>. \r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2011Q2\/AccessBusElement.png\" alt=\"Configuring the Data Store Write to access only one element of a bus\"><\/p>\r\n\r\n<p>Configured that way, the Data Store Write and Data Store Read have one port per assigned element, and the names of the selected elements are displayed on the blocks. Here is what a simple model looks like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2011Q2\/DSM_model.png\" alt=\"Example model illustrating how to access specific elements of DSM\"><\/p>\r\n\r\n<p><strong>Generated Code<\/strong><\/p>\r\n\r\n<p>If you generate code for the model, you will notice that this way to read and write memory is efficient, avoiding extra data copies.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2011Q2\/DSM_code.png\" alt=\"Example code generated for data store\"><\/p>\r\n\r\n<p>To learn more about this feature, look at the documentation titled <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/ug\/f4-146588.html#bsvjg1i\">Accessing Specific Bus and Matrix Elements<\/a>. You will find out that it supports the previously existing features of buses and data store, including global data store, access from the MATLAB Function block and arrays of buses.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Did you have a need to pass bus signal through data store in the past? Are you going to take advantage of this feature?  Let us know by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=142&amp;#comment\">comment here<\/a>.<\/p>\r\n\r\n","protected":false},"excerpt":{"rendered":"<p>In Simulink R2011a, Data Store blocks have been improved to support bus signals and more.  To give you a quick idea, these enhancements can transform the syntax you are used to on the left to what... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2011\/06\/07\/data-store-enhancement-accessing-specific-bus-and-array-elements\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,11,16],"tags":[201,12,200,202],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/142"}],"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=142"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}