{"id":39,"date":"2006-06-02T13:40:55","date_gmt":"2006-06-02T18:40:55","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=39"},"modified":"2017-04-25T11:12:45","modified_gmt":"2017-04-25T16:12:45","slug":"structures-and-comma-separated-lists","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2006\/06\/02\/structures-and-comma-separated-lists\/","title":{"rendered":"Structures and Comma-Separated Lists"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I have seen an increasing number of questions on structures and extracting information from them in a vectorized way.  Though\r\n         I've already covered portions of this topic in earlier posts, I'll try to bring together a coherent view here.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Simple Structure Array<\/a><\/li>\r\n         <li><a href=\"#9\">Converting Numeric Structure Fields to a Variable<\/a><\/li>\r\n         <li><a href=\"#12\">Converting String Structure Fields to a Cell Array<\/a><\/li>\r\n         <li><a href=\"#14\">References<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Simple Structure Array<a name=\"1\"><\/a><\/h3>\r\n   <p>Let's work with a simple structure array.  By simple here, I mean one that does not have nested structures inside (though\r\n      I don't believe there's any such defined term in MATLAB).  We can use the function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/struct.html\"><tt>struct<\/tt><\/a> to create one or we can use direct notation.  The following constructs are for <tt>s1<\/tt> and <tt>s2<\/tt> are equivalent.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">clear\r\ns1 = struct(<span style=\"color: #A020F0\">'name'<\/span>,<span style=\"color: #A020F0\">'Loren'<\/span>,<span style=\"color: #A020F0\">'FavoriteNumber'<\/span>,17)\r\ns2.name = <span style=\"color: #A020F0\">'Loren'<\/span>;\r\ns2.FavoriteNumber = 17<\/pre><pre style=\"font-style:oblique\">s1 = \r\n              name: 'Loren'\r\n    FavoriteNumber: 17\r\ns2 = \r\n              name: 'Loren'\r\n    FavoriteNumber: 17\r\n<\/pre><p>Are they the same?<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">isequal(s1,s2)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     1\r\n<\/pre><p>Now let's add more people to the \"database,\" along with their favorite numbers. See <a href=\"http:\/\/en.wikipedia.org\/wiki\/The_Answer_to_Life%2C_the_Universe_and_Everything#The_Ultimate_Answer\">this reference<\/a> for illumination on the structure inputs.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s1(end+1).name = <span style=\"color: #A020F0\">'Douglas'<\/span>;\r\ns1(end).FavoriteNumber = 42;\r\ns2(end+1) = struct(<span style=\"color: #A020F0\">'name'<\/span>,<span style=\"color: #A020F0\">'Douglas'<\/span>,<span style=\"color: #A020F0\">'FavoriteNumber'<\/span>,42);\r\nisequal(s1,s2)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     1\r\n<\/pre><p>What happens when we look at one of these, say <tt>s1<\/tt>?\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s1<\/pre><pre style=\"font-style:oblique\">s1 = \r\n1x2 struct array with fields:\r\n    name\r\n    FavoriteNumber\r\n<\/pre><p>We see that it's a <tt>struct<\/tt> with size 1x2, and fields named <tt>name<\/tt> and <tt>FavoriteNumber<\/tt>.  We can also see the first element, <tt>s1(1)<\/tt> and the associated values, since they are not too large to display.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s1(1)<\/pre><pre style=\"font-style:oblique\">ans = \r\n              name: 'Loren'\r\n    FavoriteNumber: 17\r\n<\/pre><p>Let's try digging a little deeper now and see if we can group the data differently and collect it into some other MATLAB arrays.\r\n       Start with the names.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s1.name<\/pre><pre style=\"font-style:oblique\">ans =\r\nLoren\r\nans =\r\nDouglas\r\n<\/pre><p>When I display <tt>s1.name<\/tt>, I see <tt>ans =<\/tt> displayed twice, once for each element in the struct array.  It's as if I executed this code:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s1(1).name,s1(2).name<\/pre><pre style=\"font-style:oblique\">ans =\r\nLoren\r\nans =\r\nDouglas\r\n<\/pre><p>See how I wrote that last expression?  It was really 2 expressions in this case, separated by one of the usual MATLAB statement\r\n      separators, the comma, hence the term you see in the documentation \"comma-separated list.\"\r\n   <\/p>\r\n   <h3>Converting Numeric Structure Fields to a Variable<a name=\"9\"><\/a><\/h3>\r\n   <p>Now I want to take the output from a specific field in the structure array and place the values together in one MATLAB variable.\r\n       I can do this with\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>a <tt>for<\/tt> loop,\r\n         <\/li>\r\n         <li>using the function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/deal.html\"><tt>deal<\/tt><\/a>,\r\n         <\/li>\r\n         <li>by taking advantage of notation new in Release 14.<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>I'll show all 3; my preference is for the newest notation, in part because of its compactness and clarity of intent, but it\r\n      should also be a bit faster in general.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">ls1 = length(s1);\r\n\r\n<span style=\"color: #228B22\">% for<\/span>\r\nnumFor = zeros(1,ls1);\r\n<span style=\"color: #0000FF\">for<\/span> ind=1:ls1\r\n    numFor(ind) = s1(ind).FavoriteNumber;\r\n<span style=\"color: #0000FF\">end<\/span>\r\n\r\n<span style=\"color: #228B22\">% deal<\/span>\r\n[numDeal(1), numDeal(2)] = deal(s1.FavoriteNumber);\r\n\r\n<span style=\"color: #228B22\">% R14 notation, Loren's preferred method!<\/span>\r\nnumFavorite = [s1.FavoriteNumber];<\/pre><p>Check for correct answers.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">isequal(numFor, numDeal, numFavorite)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     1\r\n<\/pre><p>The solution using <tt>deal<\/tt> requires you know how many outputs you want, which can be awkward to write in an automated way. I can place the results into\r\n      a cell array and then convert that to a numeric array. Here, I create a comma-separated list of output values, and then place\r\n      them inside <tt>[]<\/tt> to build the output cell array in <tt>numDealC<\/tt>.  Then I convert this cell array to a numeric one using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/cell2mat.html\"><tt>cell2mat<\/tt><\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[numDealC{1:ls1}] = deal(s1.FavoriteNumber);\r\nnumDeal2 = cell2mat(numDealC);\r\nisequal(numFavorite, numDeal2)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     1\r\n<\/pre><h3>Converting String Structure Fields to a Cell Array<a name=\"12\"><\/a><\/h3>\r\n   <p>Now let's work on the names.  Since they are different lengths, the names probably belong in a cell array of strings.  Using\r\n      the same idea of having a comma-separated list for the output values, I place each output into a cell.  The right-hand side\r\n      is also a comma-separated list, and, with Release 14 (MATLAB 7), I can parcel out these values to multiple output values without\r\n      using <tt>deal<\/tt> or some other means of distribution. Remember, each cell in a cell array is itself a MATLAB array.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[names{1:ls1}] = s1.name<\/pre><pre style=\"font-style:oblique\">names = \r\n    'Loren'    'Douglas'\r\n<\/pre>\r\n<p>I could have easily done this instead (thanks to John's comment for the reminder).\r\n\r\n<\/p>\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">names = {s1.name}<\/pre><pre style=\"font-style:oblique\">names = \r\n    'Loren'    'Douglas'\r\n<\/pre>\r\n<p>I now have a cell array of string names and each name corresponds to the associated value in the <tt>numFavorite<\/tt> array.  I encourage you to check out the references here, and to post any follow-up thoughts.\r\n   <\/p>\r\n   <h3>References<a name=\"14\"><\/a><\/h3>\r\n   \r\n   <p><b>Blog Articles<\/b><\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=6\">Use Dynamic Field References<\/a><\/li>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=17\">What&#8217;s the Big deal?<\/a><\/li>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=16\">Existence in MATLAB<\/a><\/li>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=26\">Working with structs<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   \r\n   <p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br>\r\n      Published with MATLAB&reg; 7.2<br><\/p>\r\n<\/div>\r\n<!--\r\n##### SOURCE BEGIN #####\r\n%% Structures and Comma-Separated Lists\r\n% I have seen an increasing number of questions on structures and extracting\r\n% information from them in a vectorized way.  Though I've already covered\r\n% portions of this topic in earlier posts, I'll try to bring together a\r\n% coherent view here.\r\n%% Simple Structure Array\r\n% Let's work with a simple structure array.  By simple here, I mean one\r\n% that does not have nested structures inside (though I don't believe\r\n% there's any such defined term in MATLAB).  We can use the function\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/struct.html |struct|>\r\n% to create one or we can use direct notation.  The following constructs\r\n% are for |s1| and |s2| are equivalent.\r\nclear\r\ns1 = struct('name','Loren','FavoriteNumber',17)\r\ns2.name = 'Loren';\r\ns2.FavoriteNumber = 17\r\n%%\r\n% Are they the same?\r\nisequal(s1,s2)\r\n%%\r\n% Now let's add more people to the \"database,\" along with their favorite\r\n% numbers.\r\n% See\r\n% <http:\/\/en.wikipedia.org\/wiki\/The_Answer_to_Life%2C_the_Universe_and_Everything#The_Ultimate_Answer this reference>\r\n% for illumination on the structure inputs.\r\ns1(end+1).name = 'Douglas'; \r\ns1(end).FavoriteNumber = 42;\r\ns2(end+1) = struct('name','Douglas','FavoriteNumber',42); \r\nisequal(s1,s2)\r\n%%\r\n% What happens when we look at one of these, say |s1|?\r\ns1\r\n%%\r\n% We see that it's a |struct| with size 1x2, and fields named |name|\r\n% and |FavoriteNumber|.  We can also see the first element, |s1(1)| and the\r\n% associated values, since they are not too large to display.\r\ns1(1)\r\n%%\r\n% Let's try digging a little deeper now and see if we can group the data\r\n% differently and collect it into some other MATLAB arrays.  Start with the\r\n% names.\r\ns1.name\r\n%%\r\n% When I display |s1.name|, I see |ans =| displayed twice, once for each\r\n% element in the struct array.  It's as if I executed this code:\r\ns1(1).name,s1(2).name\r\n%%\r\n% See how I wrote that last expression?  It was really 2 expressions in\r\n% this case, separated by one of the usual MATLAB statement separators, the\r\n% comma, hence the term you see in the documentation \"comma-separated\r\n% list.\"\r\n%% Converting Numeric Structure Fields to a Variable\r\n% Now I want to take the output from a specific field in the structure\r\n% array and place the values together in one MATLAB variable.  I can do\r\n% this with\r\n% \r\n% * a |for| loop, \r\n% * using the function \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/deal.html |deal|>,\r\n% * by taking advantage of notation new in Release 14.\r\n%\r\n% I'll show all 3;\r\n% my preference is for the newest notation, in part because of its\r\n% compactness and clarity of intent, but it should also be a bit faster in\r\n% general.  \r\nls1 = length(s1);\r\n\r\n% for\r\nnumFor = zeros(1,ls1);\r\nfor ind=1:ls1\r\n    numFor(ind) = s1(ind).FavoriteNumber;\r\nend\r\n\r\n% deal\r\n[numDeal(1), numDeal(2)] = deal(s1.FavoriteNumber);\r\n\r\n% R14 notation, Loren's preferred method!\r\nnumFavorite = [s1.FavoriteNumber];\r\n%%\r\n% Check for correct answers.\r\nisequal(numFor, numDeal, numFavorite)\r\n\r\n%%\r\n% The solution using |deal| requires you know how many outputs\r\n% you want, which can be awkward to write in an automated way. I can place\r\n% the results into a cell array and then convert that to a numeric array.\r\n% Here, I create a comma-separated list of output values, and then place\r\n% them inside |[]| to build the output cell array in |numDealC|.  Then I\r\n% convert this cell array to a numeric one using\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/cell2mat.html |cell2mat|>.\r\n[numDealC{1:ls1}] = deal(s1.FavoriteNumber);\r\nnumDeal2 = cell2mat(numDealC);\r\nisequal(numFavorite, numDeal2)\r\n%% Converting String Structure Fields to a Cell Array\r\n% Now let's work on the names.  Since they are different lengths, the names\r\n% probably belong in a cell array of strings.  Using the same idea of\r\n% having a comma-separated list for the output values, I place each output\r\n% into a cell.  The right-hand side is also a comma-separated list, and,\r\n% with Release 14 (MATLAB 7), I can parcel out these values to multiple\r\n% output values without using |deal| or some other means of distribution.\r\n% Remember, each cell in a cell array is itself a MATLAB array.\r\n[names{1:ls1}] = s1.name\r\n%%\r\n% I now have a cell array of string names and each name corresponds to the\r\n% associated value in the |numFavorite| array.  I encourage you to check\r\n% out the references here, and to <http:?p=39\/Respond post> any follow-up thoughts.\r\n%% References\r\n% *Documentation*\r\n%\r\n% * <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f2-88951.html Structures>\r\n% * <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f2-41859.html Using Dynamic Field Names>\r\n% * <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f0-56475.html Comma-Separated Lists>\r\n% * <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f0-47967.html Generating a List from a Structure>\r\n% * <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f0-48002.html How to Use the Comma-Separated Lists>\r\n%\r\n% *Blog Articles*\r\n%\r\n% * <https:\/\/blogs.mathworks.com\/loren\/?p=6 Use Dynamic Field References>\r\n% * <https:\/\/blogs.mathworks.com\/loren\/?p=17 What\u00e2\u20ac\u2122s the Big deal?>\r\n% * <https:\/\/blogs.mathworks.com\/loren\/?p=16 Existence in MATLAB>\r\n% * <https:\/\/blogs.mathworks.com\/loren\/?p=26 Working with structs>\r\n%\r\n% *Newsletter and Digest Articles*\r\n%\r\n% These articles are older, and, in some cases, the information is not\r\n% quite up to date.  For example, the examples that use\r\n% |deal|\r\n% work fine, but |deal| is no longer necessary in as many instances as it\r\n% used to be. See the\r\n% <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/rn\/r14_v7_prog6.html#1013499 R14 Release Notes> for more information on this.\r\n%\r\n% * <https:\/\/www.mathworks.com\/company\/newsletter\/spring01\/patterns.shtml Simplify your code with comma-separated lists>\r\n% * <https:\/\/www.mathworks.com\/company\/newsletter\/win98\/win98tips.shtml Exploiting the comma-separated list>\r\n% * <https:\/\/www.mathworks.com\/company\/digest\/june98\/deal.shtml Getting the most out of the deal function>\r\n% \r\n\r\n##### SOURCE END #####\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I have seen an increasing number of questions on structures and extracting information from them in a vectorized way.  Though\r\n         I've already covered portions of this topic in... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/06\/02\/structures-and-comma-separated-lists\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,6,5],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/39"}],"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=39"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":2319,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/39\/revisions\/2319"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}