{"id":88,"date":"2007-04-19T06:56:32","date_gmt":"2007-04-19T11:56:32","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2007\/04\/19\/vectorizing-access-to-an-array-of-structures\/"},"modified":"2016-08-03T14:37:51","modified_gmt":"2016-08-03T19:37:51","slug":"vectorizing-access-to-an-array-of-structures","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/04\/19\/vectorizing-access-to-an-array-of-structures\/","title":{"rendered":"Vectorizing Access to an Array of Structures"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Though I have written about this topic <a href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/11\/24\/working-with-arrays-of-structures\/\">before<\/a>, I continue to get questions on working with arrays of structures.  So I thought I would focus on that alone today.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Recent Sample Question<\/a><\/li>\r\n         <li><a href=\"#2\">The Answer<\/a><\/li>\r\n         <li><a href=\"#7\">User Solution<\/a><\/li>\r\n         <li><a href=\"#8\">Comments?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Recent Sample Question<a name=\"1\"><\/a><\/h3>\r\n   <p>Recently there was a question on the <a href=\"\">newsgroup<\/a> about how to vectorize the access to elements in a structure array.  And also got an email from a customer on this topic.\r\n       Here is one customer's description of his problem:\r\n   <\/p><pre>    In a nutshell, what I am trying to do (in as few lines of code as\r\n    possible) is:<\/pre><pre>    state = an array of structs, say N items each with (for example)\r\n    components x and y.<\/pre><pre>    In my case 'state' is reasonably complicated and hence does not\r\n    warrant the use of a simple 2 x N matrix.<\/pre><pre>    for i=2:N\r\n       state(i)=markovmodel(state(i-1)); % 1. Access individual structs\r\n    end<\/pre><pre>    plot(state.x) % 2. Access all of the entries of one element as  vector.<\/pre><h3>The Answer<a name=\"2\"><\/a><\/h3>\r\n   <p>Let's create an array of structures first.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">states(1).x = 3;\r\nstates(2).x = 4;\r\nstates(3).x = 5;\r\nstates(1).y = 42;\r\nstates(2).y = 99;\r\nstates(3).y = 0;\r\nstates<\/pre><pre style=\"font-style:oblique\">states = \r\n1x3 struct array with fields:\r\n    x\r\n    y\r\n<\/pre><p>Let's see what's in states.x<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">states.x<\/pre><pre style=\"font-style:oblique\">ans =\r\n     3\r\nans =\r\n     4\r\nans =\r\n     5\r\n<\/pre><p>With an array of structs, you can gather the <tt>x<\/tt> values using\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">allxvals = [states.x]<\/pre><pre style=\"font-style:oblique\">allxvals =\r\n     3     4     5\r\n<\/pre><p>This is because <tt>states.x<\/tt> produces a comma-separated list. How do I know this?  Because, if I leave the semi-colon off, I see a bunch of output with <tt>ans =<\/tt>; in other words, I am getting multiple output values.  How do I collect multiple values into an array?  I can use either\r\n      square brackets or curly braces.\r\n   <\/p>\r\n   <p>Using the square brackets, <tt>[]<\/tt>, just places that list into between the brackets, creating an array of one type, in this case, doubles. You can do likewise\r\n      with struct arrays with fields that are more suitable for a cell array using curly braces, <tt>{}<\/tt> in place of the square brackets.\r\n   <\/p>\r\n   <h3>User Solution<a name=\"7\"><\/a><\/h3>\r\n   <p>If instead of using<\/p><pre>    plot(state.x)\r\nabove, the user replaced this with<\/pre><pre>    plot([state.x])<\/pre><p>the informationg gets plotted correctly.<\/p>\r\n   <h3>Comments?<a name=\"8\"><\/a><\/h3>\r\n   <p>My question to you is why this question keeps getting asked.  Is the concept unusual enough that people don't know how to\r\n      even look for the information on-line?  What can we do at The MathWorks to make this more visible?  Please pass along your\r\n      thoughts <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=88#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_df665f5339be468c870d22bb5c57891d() {\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='df665f5339be468c870d22bb5c57891d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' df665f5339be468c870d22bb5c57891d';\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 = 'Loren Shure';\r\n        copyright = 'Copyright 2007 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_df665f5339be468c870d22bb5c57891d()\"><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.4<br><\/p>\r\n<\/div>\r\n<!--\r\ndf665f5339be468c870d22bb5c57891d ##### SOURCE BEGIN #####\r\n%% Vectorizing Access to an Array of Structures\r\n% Though I have written about this topic\r\n% <https:\/\/blogs.mathworks.com\/loren\/2006\/11\/24\/working-with-arrays-of-structures\/ before>,\r\n% I continue to get questions on working with arrays of structures.  So I\r\n% thought I would focus on that alone today.\r\n%% Recent Sample Question\r\n% Recently there was a question on the \r\n% < newsgroup>\r\n% about how to vectorize the access to elements in a structure array.  And\r\n% also got an email from a customer on this topic.  Here is one customer's\r\n% description of his problem:\r\n%\r\n%      In a nutshell, what I am trying to do (in as few lines of code as\r\n%      possible) is:\r\n% \r\n%      state = an array of structs, say N items each with (for example) \r\n%      components x and y.\r\n% \r\n%      In my case 'state' is reasonably complicated and hence does not \r\n%      warrant the use of a simple 2 x N matrix.\r\n% \r\n%      for i=2:N\r\n%         state(i)=markovmodel(state(i-1)); % 1. Access individual structs \r\n%      end\r\n% \r\n%      plot(state.x) % 2. Access all of the entries of one element as  vector.\r\n%% The Answer\r\n% Let's create an array of structures first.\r\nstates(1).x = 3;\r\nstates(2).x = 4;\r\nstates(3).x = 5;\r\nstates(1).y = 42;\r\nstates(2).y = 99;\r\nstates(3).y = 0;\r\nstates\r\n%%\r\n% Let's see what's in states.x\r\nstates.x\r\n%%\r\n% With an array of structs, you can gather the |x| values using\r\nallxvals = [states.x]\r\n%%\r\n% This is because |states.x| produces a \r\n% <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f0-56475.html comma-separated list>.\r\n% How do I know this?  Because, if I leave the semi-colon off, I see a\r\n% bunch of output with |ans =|; in other words, I am getting multiple\r\n% output values.  How do I collect multiple values into an array?  I can\r\n% use either square brackets or curly braces.\r\n%%\r\n% Using the square brackets, |[]|, just places that list into between\r\n% the brackets, creating an array of one type, in this case, doubles.\r\n% You can do likewise with struct arrays with fields that are more suitable\r\n% for a cell array using curly braces, |{}| in place of the square brackets.\r\n%% User Solution\r\n% If instead of using\r\n%\r\n%      plot(state.x) \r\n% above, the user replaced this with\r\n%\r\n%      plot([state.x])\r\n%\r\n% the informationg gets plotted correctly.\r\n%% Comments?\r\n% My question to you is why this question keeps getting asked.  Is the\r\n% concept unusual enough that people don't know how to even look for the\r\n% information on-line?  What can we do at The MathWorks to make this more\r\n% visible?  Please pass along your thoughts\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=88#respond here>.\r\n##### SOURCE END ##### df665f5339be468c870d22bb5c57891d\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Though I have written about this topic before, I continue to get questions on working with arrays of structures.  So I thought I would focus on that alone today.\r\n      \r\n   \r\n ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/04\/19\/vectorizing-access-to-an-array-of-structures\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,12],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/88"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":1916,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/88\/revisions\/1916"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}