{"id":245,"date":"2016-08-10T23:14:03","date_gmt":"2016-08-11T07:14:03","guid":{"rendered":"http:\/\/wonghoi.humgar.com\/blog\/?p=245"},"modified":"2016-08-11T22:56:47","modified_gmt":"2016-08-12T06:56:47","slug":"matlab-quirks-struct-with-no-fields-are-not-empty","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2016\/08\/10\/matlab-quirks-struct-with-no-fields-are-not-empty\/","title":{"rendered":"MATLAB Quirks: struct with no fields are not empty"},"content":{"rendered":"<p>As far as\u00a0struct() is concerned, I&#8217;m more inclined to using Struct of Array (SoA) over Array of Structs (AoS), unless all the use cases screams for SoA. Performance and memory overhead are the obvious reasons, but\u00a0the true\u00a0motivation for me to use SoA is that I&#8217;m thinking in terms of table-oriented programming (which I&#8217;ll discuss in later posts. See <a href=\"http:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/create-a-table.html\">table()<\/a> objects.): each field of a struct is a column\u00a0in a table (heterogeneous array).<\/p>\n<p>Since a table() is considered empty (by isempty()) if it has EITHER <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-8354ade9c79ec6a7ac658f2c3032c9df_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: 0px;\"\/> rows INCLUSIVE OR <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-8354ade9c79ec6a7ac658f2c3032c9df_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: 0px;\"\/> columns (no fields) and the default constructor creates a <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-62ced9cc47adf4fc40714069bd46adc3_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#48;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"40\" style=\"vertical-align: 0px;\"\/> table, I thought struct() would do the same. NOT TRUE!<\/p>\n<p>First of all, the default constructor of struct() gives ONE struct with NO FIELDS (so it&#8217;s supposed to correspond to a <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-edee1f9e6a1102ca744974cfed3af232_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#49;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"39\" style=\"vertical-align: 0px;\"\/> table). What&#8217;s even harder to remember is that\u00a0struct2table(struct()) gives a <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-62ced9cc47adf4fc40714069bd46adc3_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#48;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"40\" style=\"vertical-align: 0px;\"\/> table.<\/p>\n<p>The second\u00a0thing I missed is that\u00a0a struct() with NO fields is NOT empty. You can have 3 structs with NO fields! So isempty(struct()) is always false!<\/p>\n<p>I usually run into this problem when I want to seed\u00a0the execution with an empty struct() and have the loop expand the fields if the file has contents in it, and I&#8217;ll check if the seeded struct was untouched to see if I can read data from the file. Next time I will remember to call struct([]) instead of struct(). What a trap!<\/p>\n<p>At the end of the day, while struct is powerful, but I rarely find\u00a0AoS necessary to do what I wanted once table() is out. AoS has\u00a0pretty much the same restrictions as in table() that you cannot put different types in the same field across the\u00a0AoS, but table allows you to index with variables (struct&#8217;s field) or rows (struct array index) without changing the data structure (AoS &lt;-&gt; SoA). So unless it&#8217;s a performance critical piece of the code, I&#8217;ll stick with tables() for most of my struct() needs.<\/p>\n<p>&nbsp;<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_245\" class=\"pvc_stats all  \" data-element-id=\"245\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>As far as\u00a0struct() is concerned, I&#8217;m more inclined to using Struct of Array (SoA) over Array of Structs (AoS), unless all the use cases screams for SoA. Performance and memory overhead are the obvious reasons, but\u00a0the true\u00a0motivation for me to &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2016\/08\/10\/matlab-quirks-struct-with-no-fields-are-not-empty\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_245\" class=\"pvc_stats all  \" data-element-id=\"245\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[10,6],"tags":[],"class_list":["post-245","post","type-post","status-publish","format-standard","hentry","category-matlab","category-note-to-self"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=245"}],"version-history":[{"count":3,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":862,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/245\/revisions\/862"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}