{"id":304,"date":"2016-10-05T21:44:19","date_gmt":"2016-10-06T05:44:19","guid":{"rendered":"http:\/\/wonghoi.humgar.com\/blog\/?p=304"},"modified":"2019-01-22T01:14:25","modified_gmt":"2019-01-22T09:14:25","slug":"matlab-compatibility-nominal-and-ordinal-objects-since-r2013a-are-not-compatible-with-r2012b-and-before","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2016\/10\/05\/matlab-compatibility-nominal-and-ordinal-objects-since-r2013a-are-not-compatible-with-r2012b-and-before\/","title":{"rendered":"MATLAB Compatibility: nominal() and ordinal() objects since R2013a are not compatible with R2012b and before"},"content":{"rendered":"<p>In the old days (before R2013a), nominal() and ordinal() were separate parallel\u00a0classes\u00a0with astoundingly\u00a0similar structures. That means there&#8217;s a lot of copy-paste-mod going on. TMW improved on it by\u00a0consolidating the ideas into a new categorical() class, which nominal() and ordinal() derives from it.<\/p>\n<p>The documentation mentioned that nominal() and ordinal() might be deprecated in the future, but I contacted their support urging them not to. It&#8217;s not for compatibility reasons: nominal() and ordinal() captures the common use cases that these two ideas do not need to be unified, and the names themselves clearly encodes the intention.<\/p>\n<p>If the user want to\u00a0exploit the commonalities between the two, either it&#8217;s already taken care of by the parent&#8217;s public methods, or the object can be sliced to make it happen. I looked into the source code for nominal() and ordinal(): it&#8217;s pretty much a wrapper over categorical&#8217;s methods yet the\u00a0interface (input arguments) are much simpler and intuitive because we don&#8217;t have to consider all the\u00a0more general cases.<\/p>\n<p>Back to the titled topic. Because categorical()&#8217;s properties (members) are different from\u00a0pre R2013a&#8217;s nominal() and ordinal() objects, the objects created in R2012b or before cannot be loaded correctly in newer versions. That means the backward compatibility is completely broken for nominal()\/ordinal() as far as saved objects are concerned.<\/p>\n<p>There&#8217;s no good incentive to solve this problem on the TMWs side because the old nominal()\/ordinal() is short-lived and they always want everybody to upgrade.\u00a0Since I use nominal() most of the time and the ones that really need to be saved are all nominal(), I recommend the converting (&#8216;casting&#8217;) them to cellstr by<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"matlab\">&gt;&gt; A = nominal({'a','a','b','c'});\r\n&gt;&gt; A = cellstr(A)\r\nA = \r\n    'a'    'a'    'b'    'c'<\/pre>\n<p>Remember, nominal() is pretty much compressing a ton of cellstr into a few unique items and mapping the indices.\u00a0No information is lost going back and forth between cellstr() and nominal(). It&#8217;s just\u00a0a little extra computations for the conversion.<\/p>\n<p>As for ordinal(),\u00a0I rarely need to save it because order\/level\u00a0assignment is almost the very last thing in the processing chain because it changes so frequently (e.g. how would you draw the lines for <a href=\"http:\/\/www.dailymotion.com\/video\/x29jesw_gabriel-iglesias-i-m-not-fat-i-m-fluffy-6-levels-of-fatness_fun\">six levels of fatness<\/a>?), I might as well just not save it and reprocess the last step (where the code with\u00a0ordinal() sits) when I need it.<\/p>\n<p>Nonetheless, if you still want to save ordinals() instead of re-crunching\u00a0it, this time you&#8217;ll want to save it as numerical levels by casting the ordinal() into double():<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"matlab\">&gt;&gt; A = ordinal([1 2 3; 3 2 1; 2 1 3],{'low' 'medium' 'high'}, [3 1 2])\r\nA = \r\n     medium      high        low    \r\n     low         high        medium \r\n     high        medium      low    \r\n&gt;&gt; D = double(A)\r\nD =\r\n     2     3     1\r\n     1     3     2\r\n     3     2     1\r\n&gt;&gt; U = unique(A)\r\nU = \r\n     low \r\n     medium \r\n     high \r\n&gt;&gt; L = cellstr(U)\r\nL = \r\n    'low'\r\n    'medium'\r\n    'high'\r\n&gt;&gt; I = double(U)\r\nI =\r\n     1\r\n     2\r\n     3\r\n&gt;&gt; A_reconstructed = ordinal(D, L, I)\r\nA_reconstructed = \r\n     medium      high        low    \r\n     low         high        medium \r\n     high        medium      low<\/pre>\n<p>You&#8217;ll save (D, L, I) from old MATLAB and load it and reconstruct it with the triplets from the new MATLAB (I&#8217;d suggest using\u00a0structs to keep track of the triplets).\u00a0I know it&#8217;s a hairy mess!<\/p>\n<p>&nbsp;<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_304\" class=\"pvc_stats all  \" data-element-id=\"304\" 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>In the old days (before R2013a), nominal() and ordinal() were separate parallel\u00a0classes\u00a0with astoundingly\u00a0similar structures. That means there&#8217;s a lot of copy-paste-mod going on. TMW improved on it by\u00a0consolidating the ideas into a new categorical() class, which nominal() and ordinal() derives &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2016\/10\/05\/matlab-compatibility-nominal-and-ordinal-objects-since-r2013a-are-not-compatible-with-r2012b-and-before\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_304\" class=\"pvc_stats all  \" data-element-id=\"304\" 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],"tags":[],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-matlab"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/304","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=304"}],"version-history":[{"count":8,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":1273,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/304\/revisions\/1273"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}