{"id":8619,"date":"2017-06-16T09:00:57","date_gmt":"2017-06-16T13:00:57","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=8619"},"modified":"2017-06-19T11:40:06","modified_gmt":"2017-06-19T15:40:06","slug":"catstruct","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2017\/06\/16\/catstruct\/","title":{"rendered":"Catstruct"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\n   <introduction><\/p>\n<p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7842-catstruct\">catstruct<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/870065\">Jos (10584)<\/a>.\n      <\/p>\n<p>   <\/introduction><\/p>\n<p>Have you ever had two structures and wanted to combine them into one?<\/p>\n<p>The above question is kind of ambiguous.  Do you mean?<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">firststruct.x = pi\r\nsecondstruct.x = exp(1)\r\ncombinedstruct = [firststruct secondstruct]<\/pre>\n<pre style=\"font-style:oblique\">firststruct = \r\n  struct with fields:\r\n\r\n    x: 3.1416\r\nsecondstruct = \r\n  struct with fields:\r\n\r\n    x: 2.7183\r\ncombinedstruct = \r\n  1&times;2 struct array with fields:\r\n    x\r\n<\/pre>\n<p>Or?<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">\r\nthirdstruct.x = pi\r\nfourthstruct.y = exp(1)\r\ncombinedstruct = [thirdstruct fourthstruct]\r\n<\/pre>\n<pre style=\"font-style:oblique\">thirdstruct = \r\n  struct with fields:\r\n\r\n    x: 3.1416\r\nfourthstruct = \r\n  struct with fields:\r\n\r\n    y: 2.7183\r\n    <font color=\"#FF0000\">\r\nError using horzcat<br>Names of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of fields.<\/font><\/pre>\n<p>Well if you wanted the first one you&#8217;re in luck.  That worked because the structures have like fieldnames.  If they don&#8217;t, you get the error in the second one.  This is where <tt>catstruct<\/tt> comes to the rescue.\n   <\/p>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainCatstruct\/catstruct.png\"> <\/p>\n<p>No not that cat-struct.  This one!<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">combinedstruct = catstruct(thirdstruct, fourthstruct)<\/pre>\n<pre style=\"font-style:oblique\">combinedstruct = \r\n  struct with fields:\r\n\r\n    x: 3.1416\r\n    y: 2.7183\r\n<\/pre>\n<p>I discovered catstruct while working with <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/regexp.html\">regular expressions<\/a> to parse a custom text file.  I used separate regular expressions and kept ending up with multiple structs.  Catstruct made joining them into one really easy.  Here&#8217;s a representative example:\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">str = <span style=\"color: #A020F0\">\"Hello World.  15 quick brown foxes jumped over 3 2.3ft tall lazy dogs\"<\/span>;\r\n\r\n<span style=\"color: #228B22\">% Extract pieces using regular expressions.<\/span>\r\nfoxes = regexp(str, <span style=\"color: #A020F0\">'(?&lt;foxes&gt;\\d+)(?=[\\w\\s]*fox)'<\/span>, <span style=\"color: #A020F0\">'names'<\/span>)\r\ndogs = regexp(str, <span style=\"color: #A020F0\">'(?&lt;=fox\\D*)(?&lt;dogs&gt;\\d+)'<\/span>, <span style=\"color: #A020F0\">'names'<\/span>)\r\ndogheight = regexp(str, <span style=\"color: #A020F0\">'(?&lt;dogheight&gt;[\\.\\d]+)ft'<\/span>, <span style=\"color: #A020F0\">'names'<\/span>)\r\nheightunit = regexp(str, <span style=\"color: #A020F0\">'\\d(?&lt;heightunit&gt;[a-zA-Z]+)\\s'<\/span>, <span style=\"color: #A020F0\">'names'<\/span>)<\/pre>\n<pre style=\"font-style:oblique\">foxes = \r\n  struct with fields:\r\n\r\n    foxes: \"15\"\r\ndogs = \r\n  struct with fields:\r\n\r\n    dogs: \"3\"\r\ndogheight = \r\n  struct with fields:\r\n\r\n    dogheight: \"2.3\"\r\nheightunit = \r\n  struct with fields:\r\n\r\n    heightunit: \"ft\"\r\n<\/pre>\n<p>Combine.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">info = catstruct(foxes, dogs, dogheight, heightunit)<\/pre>\n<pre style=\"font-style:oblique\">info = \r\n  struct with fields:\r\n\r\n         foxes: \"15\"\r\n          dogs: \"3\"\r\n     dogheight: \"2.3\"\r\n    heightunit: \"ft\"\r\n<\/pre>\n<h3>Comments<a name=\"6\"><\/a><\/h3>\n<p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=8619#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7842-catstruct#comments\">comment<\/a> for Jos.\n   <\/p>\n<p><script language=\"JavaScript\">\n<!--\n\n    function grabCode_e805dc1489d1472085adb4fa4d403920() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='e805dc1489d1472085adb4fa4d403920 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e805dc1489d1472085adb4fa4d403920';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Sean de Wolski';\n        copyright = 'Copyright 2017 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n<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\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }   \n      \n-->\n<\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><a href=\"javascript:grabCode_e805dc1489d1472085adb4fa4d403920()\"><span style=\"font-size: x-small;        font-style: italic;\">Get<br \/>\n            the MATLAB code<br \/>\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>      Published with MATLAB&reg; R2017a<\/p>\n<\/div>\n<p><!--\ne805dc1489d1472085adb4fa4d403920 ##### SOURCE BEGIN #####\n%% Catstruct\n%\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7842-catstruct catstruct> by\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/870065 Jos (10584)>.\n% \n\n%% \n% Have you ever had two structures and wanted to combine them into one?\n%\n% The above question is kind of ambiguous.  Do you mean?\n\nfirststruct.x = pi\nsecondstruct.x = exp(1)\ncombinedstruct = [firststruct secondstruct]\n\n%% \n% Or?\ntry\nthirdstruct.x = pi\nfourthstruct.y = exp(1)\ncombinedstruct = [thirdstruct fourthstruct]\ncatch ME\n   fprintf(2,ME.message); \nend\n%% \n%\n% Well if you wanted the first one you're in luck.  That worked because the\n% structures have like fieldnames.  If they don't, you get the error in the\n% second one.  This is where |catstruct| comes to the rescue.\n%\n% <<catstruct.png>>\n%\n% No not that cat-struct.  This one!\n\ncombinedstruct = catstruct(thirdstruct, fourthstruct)\n\n%% \n% I discovered catstruct while working with\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/regexp.html regular\n% expressions> to parse a custom text file.  I used separate regular\n% expressions and kept ending up with multiple structs.  Catstruct made\n% joining them into one really easy.  Here's a representative example:\n\nstr = \"Hello World.  15 quick brown foxes jumped over 3 2.3ft tall lazy dogs\";\n\n% Extract pieces using regular expressions.\nfoxes = regexp(str, '(?<foxes>\\d+)(?=[\\w\\s]*fox)', 'names')\ndogs = regexp(str, '(?<=fox\\D*)(?<dogs>\\d+)', 'names')\ndogheight = regexp(str, '(?<dogheight>[\\.\\d]+)ft', 'names')\nheightunit = regexp(str, '\\d(?<heightunit>[a-zA-Z]+)\\s', 'names')\n\n%%\n% Combine.\ninfo = catstruct(foxes, dogs, dogheight, heightunit)\n\n%% Comments\n% \n% Give it a try and let us know what you think\n% <https:\/\/blogs.mathworks.com\/pick\/?p=8619#respond here> or leave a\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7842-catstruct#comments\n% comment> for Jos.\n##### SOURCE END ##### e805dc1489d1472085adb4fa4d403920\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/pick\/files\/infostruct.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div>\n<p>Sean&#8216;s pick this week is catstruct by Jos (10584).<\/p>\n<p>Have you ever had two structures and wanted to combine them into one?<br \/>\nThe above question is kind of ambiguous.  Do you&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2017\/06\/16\/catstruct\/\">read more >><\/a><\/p>\n","protected":false},"author":87,"featured_media":8626,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,28],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8619"}],"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\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=8619"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8619\/revisions"}],"predecessor-version":[{"id":8653,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8619\/revisions\/8653"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media\/8626"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=8619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=8619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=8619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}