{"id":2922,"date":"2011-08-05T13:08:43","date_gmt":"2011-08-05T13:08:43","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2011\/08\/05\/converting-variables-to-structures-and-vice-versa\/"},"modified":"2011-08-05T13:08:43","modified_gmt":"2011-08-05T13:08:43","slug":"converting-variables-to-structures-and-vice-versa","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2011\/08\/05\/converting-variables-to-structures-and-vice-versa\/","title":{"rendered":"Converting variables to structures, and vice versa"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31532-pack---unpack-variables-to---from-structures-with-enhanced-functionality\"><tt>v2struct<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/148428\">Adi Navve<\/a>.\r\n   <\/p>\r\n   <p>I have a special liking for convenience\/utility functions, because they make my life easier. For example, here's something\r\n      that many users have probably learned to do early on:\r\n   <\/p><pre>  plot(x, y, 'ro:')<\/pre><p><p>Did you know that <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/plot.html\"><tt>plot<\/tt><\/a> is a high-level, convenience function for a much more flexible, low-level function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/line.html\"><tt>line<\/tt><\/a>?\r\n   <\/p><pre>  line(x, y, 'Color', 'r', 'Marker', 'o', 'LineStyle', ':')<\/pre><p><p>Adi's <tt>v2struct<\/tt> is a function that makes converting between <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/matlab_prog\/br04bw6-38.html\">structures<\/a> and individual variables easier.\r\n   <\/p>\r\n   <p><b>Variables to a Structure<\/b><\/p>\r\n   <p>Suppose you have a bunch of variables that you want to combine into a single structure:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">aString = <span style=\"color: #A020F0\">'August 5, 2011'<\/span>;\r\naScalar = pi;\r\naVector = rand(1, 5);<\/pre><p><p><tt>v2struct<\/tt> automatically uses the names of the variables as the field names:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = v2struct(aString, aScalar, aVector)<\/pre><pre style=\"font-style:oblique\">s = \r\n    aString: 'August 5, 2011'\r\n    aScalar: 3.1416\r\n    aVector: [0.8407 0.2543 0.8143 0.2435 0.9293]\r\n<\/pre><p><p><b>Structure to Variables<\/b><\/p>\r\n   <p>The function works the other way also. Suppose you have a structure like this:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s2 = struct(<span style=\"color: #A020F0\">'var1'<\/span>, 1, <span style=\"color: #A020F0\">'var2'<\/span>, [1 2 3], <span style=\"color: #A020F0\">'var3'<\/span>, <span style=\"color: #A020F0\">'hello'<\/span>)<\/pre><pre style=\"font-style:oblique\">s2 = \r\n    var1: 1\r\n    var2: [1 2 3]\r\n    var3: 'hello'\r\n<\/pre><p><p>You can create variables from the fields of the structure:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">v2struct(s2)\r\nwhos <span style=\"color: #A020F0\">var*<\/span><\/pre><pre style=\"font-style:oblique\">  Name      Size            Bytes  Class     Attributes\r\n\r\n  var1      1x1                 8  double              \r\n  var2      1x3                24  double              \r\n  var3      1x5                10  char                \r\n\r\n<\/pre><p>In addition to these basic uses, <tt>v2struct<\/tt> provides advanced functionalities, such as\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>creating structures with field names different from variable names<\/li>\r\n         <li>updating fields of an existing structure<\/li>\r\n         <li>extracting only specific fields of a structure<\/li>\r\n         <li>extracting fields with a different variable name<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p><p>What I also like about Adi's function is that there is a very extensive help section with lots of examples. I can see he has\r\n      put in a lot of effort in making this easy to use.\r\n   <\/p>\r\n   <p><b>Comments<\/b><\/p>\r\n   <p>Let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2922#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31532-pack---unpack-variables-to---from-structures-with-enhanced-functionality#comments\">comment<\/a> for Adi.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_88b752b3c40c4e3ea96417e715ceadee() {\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='88b752b3c40c4e3ea96417e715ceadee ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 88b752b3c40c4e3ea96417e715ceadee';\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 = 'Jiro Doke';\r\n        copyright = 'Copyright 2011 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_88b752b3c40c4e3ea96417e715ceadee()\"><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.12<br><\/p>\r\n<\/div>\r\n<!--\r\n88b752b3c40c4e3ea96417e715ceadee ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31532-pack---unpack-variables-to---from-structures-with-enhanced-functionality |v2struct|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/148428 Adi\r\n% Navve>.\r\n%\r\n% I have a special liking for convenience\/utility functions, because they\r\n% make my life easier. For example, here's something that many users have\r\n% probably learned to do early on:\r\n%\r\n%    plot(x, y, 'ro:')\r\n%\r\n% Did you know that <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/plot.html\r\n% |plot|> is a high-level, convenience function for a much more flexible,\r\n% low-level function <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/line.html\r\n% |line|>?\r\n%\r\n%    line(x, y, 'Color', 'r', 'Marker', 'o', 'LineStyle', ':')\r\n%\r\n% Adi's |v2struct| is a function that makes converting between\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/matlab_prog\/br04bw6-38.html\r\n% structures> and individual variables easier.\r\n%\r\n% *Variables to a Structure*\r\n%\r\n% Suppose you have a bunch of variables that you want to combine into a\r\n% single structure:\r\n\r\naString = 'August 5, 2011';\r\naScalar = pi;\r\naVector = rand(1, 5);\r\n\r\n%%\r\n% |v2struct| automatically uses the names of the variables as the field\r\n% names:\r\n\r\ns = v2struct(aString, aScalar, aVector)\r\n\r\n%%\r\n% *Structure to Variables*\r\n%\r\n% The function works the other way also. Suppose you have a structure like\r\n% this:\r\n\r\ns2 = struct('var1', 1, 'var2', [1 2 3], 'var3', 'hello')\r\n\r\n%%\r\n% You can create variables from the fields of the structure:\r\n\r\nv2struct(s2)\r\nwhos var*\r\n\r\n%%\r\n% In addition to these basic uses, |v2struct| provides advanced\r\n% functionalities, such as\r\n%\r\n% * creating structures with field names different from variable names\r\n% * updating fields of an existing structure\r\n% * extracting only specific fields of a structure\r\n% * extracting fields with a different variable name\r\n%\r\n% What I also like about Adi's function is that there is a very extensive\r\n% help section with lots of examples. I can see he has put in a lot of\r\n% effort in making this easy to use.\r\n%\r\n% *Comments*\r\n%\r\n% Let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2922#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31532-pack---unpack-variables-to---from-structures-with-enhanced-functionality#comments\r\n% comment> for Adi.\r\n\r\n##### SOURCE END ##### 88b752b3c40c4e3ea96417e715ceadee\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Jiro's pick this week is v2struct by Adi Navve.\r\n   \r\n   I have a special liking for convenience\/utility functions, because they make my life easier. For example, here's something\r\n      that... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2011\/08\/05\/converting-variables-to-structures-and-vice-versa\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2922"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2922"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2922\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}