{"id":232,"date":"2010-05-13T10:56:24","date_gmt":"2010-05-13T10:56:24","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2010\/05\/13\/rename-a-field-in-a-structure-array\/"},"modified":"2018-05-06T19:18:39","modified_gmt":"2018-05-07T00:18:39","slug":"rename-a-field-in-a-structure-array","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2010\/05\/13\/rename-a-field-in-a-structure-array\/","title":{"rendered":"Rename a Field in a Structure Array"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I'm Matthew Simoneau, a software developer at MathWorks focusing on technical communication and social computing.<\/p>\r\n      <p>My friend Bryan May, an occasional MATLAB programmer, called me with a question the other day. He was working with a structure\r\n         array and wanted to rename one of the fields. My scan of the documentation came up empty. MATLAB has a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/setfield.html\"><tt>setfield<\/tt><\/a> and a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/rmfield.html\"><tt>rmfield<\/tt><\/a>, but not a \"rename field\". This started me thinking about the best way to implement this in MATLAB.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Create a Sample Structure Array<\/a><\/li>\r\n         <li><a href=\"#2\">Using STRUCT2CELL and CELL2STRUCT<\/a><\/li>\r\n         <li><a href=\"#3\">Using List Expansions and DEAL<\/a><\/li>\r\n         <li><a href=\"#4\">No DEAL Required<\/a><\/li>\r\n         <li><a href=\"#5\">Generalization<\/a><\/li>\r\n         <li><a href=\"#6\">Conclusion<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Create a Sample Structure Array<a name=\"1\"><\/a><\/h3>\r\n   <p>First, lets create a simple structure array.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">clear <span style=\"color: #A020F0\">a<\/span>\r\na(1).foo = 1;\r\na(1).bar = <span style=\"color: #A020F0\">'one'<\/span>;\r\na(2).foo = 2;\r\na(2).bar = <span style=\"color: #A020F0\">'two'<\/span>;\r\na(3).foo = 3;\r\na(3).bar = <span style=\"color: #A020F0\">'three'<\/span>;\r\ndisp(a)<\/pre><pre style=\"font-style:oblique\">1x3 struct array with fields:\r\n    foo\r\n    bar\r\n<\/pre><h3>Using STRUCT2CELL and CELL2STRUCT<a name=\"2\"><\/a><\/h3>\r\n <p>The first technique that came to mind was to use the combination of <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/struct2cell.html\"><tt>struct2cell<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/cell2struct.html\"><tt>cell2struct<\/tt><\/a>. Here we convert the structure to two cell arrays, one containing the fieldnames <tt>f<\/tt> and one containing the values <tt>v<\/tt>. We find the field in <tt>f<\/tt> and rename it, then put the structure back together.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">f = fieldnames(a);\r\nv = struct2cell(a);\r\nf{strmatch(<span style=\"color: #A020F0\">'bar'<\/span>,f,<span style=\"color: #A020F0\">'exact'<\/span>)} = <span style=\"color: #A020F0\">'baz'<\/span>;\r\na = cell2struct(v,f);\r\ndisp(a)<\/pre><pre style=\"font-style:oblique\">1x3 struct array with fields:\r\n    foo\r\n    baz\r\n<\/pre><h3>Using List Expansions and DEAL<a name=\"3\"><\/a><\/h3>\r\n   <p>Thinking a bit more, I came up with a way to do this a bit more \"in place\". Comma-separated list expansion is a powerful concept\r\n      in MATLAB. I knew I could generate one with the <tt>a(:).baz<\/tt> notation, and that I could use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/deal.html\"><tt>deal<\/tt><\/a> to assign them back into another comma-separated list.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[a(:).qux] = deal(a(:).baz);\r\na = rmfield(a,<span style=\"color: #A020F0\">'baz'<\/span>);\r\ndisp(a)<\/pre><pre style=\"font-style:oblique\">1x3 struct array with fields:\r\n    foo\r\n    qux\r\n<\/pre><h3>No DEAL Required<a name=\"4\"><\/a><\/h3>\r\n   <p>Scott French pointed out to me that, as of MATLAB 7, the <tt>deal<\/tt> was no longer necessary.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[a.quux] = a.qux;\r\na = rmfield(a,<span style=\"color: #A020F0\">'qux'<\/span>);\r\ndisp(a)<\/pre><pre style=\"font-style:oblique\">1x3 struct array with fields:\r\n    foo\r\n    quux\r\n<\/pre><h3>Generalization<a name=\"5\"><\/a><\/h3>\r\n   <p>Further, Kenneth Eaton commented that this technique generalizes nicely using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/matlab_prog\/br04bw6-38.html#br1v5cc-1\">dynamic field names<\/a>, introduced in MATLAB 6.5.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">oldField = <span style=\"color: #A020F0\">'quux'<\/span>;\r\nnewField = <span style=\"color: #A020F0\">'corge'<\/span>;\r\n[a.(newField)] = a.(oldField);\r\na = rmfield(a,oldField);\r\ndisp(a)<\/pre><pre style=\"font-style:oblique\">1x3 struct array with fields:\r\n    foo\r\n    corge\r\n<\/pre><h3>Conclusion<a name=\"6\"><\/a><\/h3>\r\n   <p>My guess is that the no-deal technique used in the last two sections is the most efficient in most circumstances, though I\r\n      haven't done any profiling. The code is certainly the cleanest in these.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_dd54717a56d14030aa414a5f966bad6e() {\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='dd54717a56d14030aa414a5f966bad6e ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' dd54717a56d14030aa414a5f966bad6e';\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 2010 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_dd54717a56d14030aa414a5f966bad6e()\"><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.10<br><\/p>\r\n<\/div>\r\n<!--\r\ndd54717a56d14030aa414a5f966bad6e ##### SOURCE BEGIN #####\r\n%% Rename a Field in a Structure Array\r\n% I'm Matthew Simoneau, a software developer at MathWorks focusing on \r\n% technical communication and social computing.\r\n% \r\n% My friend Bryan May, an occasional MATLAB programmer, called me with a\r\n% question the other day. He was working with a structure array and wanted\r\n% to rename one of the fields. My scan of the documentation came\r\n% up empty. MATLAB has a <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/setfield.html |setfield|>\r\n% and a <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/rmfield.html |rmfield|>,\r\n% but not a \"rename field\". This started me thinking about the best way to\r\n% implement this in MATLAB.\r\n\r\n\r\n%% Create a Sample Structure Array\r\n% First, lets create a simple structure array.\r\n\r\nclear a\r\na(1).foo = 1;\r\na(1).bar = 'one';\r\na(2).foo = 2;\r\na(2).bar = 'two';\r\na(3).foo = 3;\r\na(3).bar = 'three';\r\ndisp(a)\r\n\r\n\r\n%% Using STRUCT2CELL and CELL2STRUCT\r\n% The first technique that came to mind was to use the combination of \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/struct2cell.html |struct2cell|> and <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/cell2struct.html |cell2struct|>. Here we convert the structure to two\r\n% cell arrays, one containing the fieldnames |f| and one containing the\r\n% values |v|. We find the field in |f| and rename it, then put the\r\n% structure back together.\r\n\r\nf = fieldnames(a);\r\nv = struct2cell(a);\r\nf{strmatch('bar',f,'exact')} = 'baz';\r\na = cell2struct(v,f);\r\ndisp(a)\r\n\r\n\r\n%% Using List Expansions and DEAL\r\n% Thinking a bit more, I came up with a way to do this a bit more \"in\r\n% place\". Comma-separated list expansion is a powerful concept in MATLAB. I\r\n% knew I could generate one with the |a(:).baz| notation, and that I could\r\n% use <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/deal.html |deal|> to assign them back into another comma-separated list.\r\n\r\n[a(:).qux] = deal(a(:).baz);\r\na = rmfield(a,'baz');\r\ndisp(a)\r\n\r\n\r\n%% No DEAL Required\r\n% Scott French pointed out to me that, as of MATLAB 7, the |deal| was no\r\n% longer necessary.\r\n\r\n[a.quux] = a.qux;\r\na = rmfield(a,'qux');\r\ndisp(a)\r\n\r\n\r\n%% Generalization\r\n% Further, Kenneth Eaton commented that this technique generalizes nicely\r\n% using <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/matlab_prog\/br04bw6-38.html#br1v5cc-1 dynamic field names>, introduced in MATLAB 6.5.\r\n\r\noldField = 'quux';\r\nnewField = 'corge';\r\n[a.(newField)] = a.(oldField);\r\na = rmfield(a,oldField);\r\ndisp(a)\r\n\r\n\r\n%% Conclusion\r\n% My guess is that the no-deal technique used in the last two sections\r\n% is the most efficient in most circumstances, though I haven't done any\r\n% profiling. The code is certainly the cleanest in these.\r\n##### SOURCE END ##### dd54717a56d14030aa414a5f966bad6e\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I'm Matthew Simoneau, a software developer at MathWorks focusing on technical communication and social computing.\r\n      My friend Bryan May, an occasional MATLAB programmer, called me... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2010\/05\/13\/rename-a-field-in-a-structure-array\/\">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],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/232"}],"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=232"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"predecessor-version":[{"id":2854,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/232\/revisions\/2854"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}