{"id":300,"date":"2016-09-20T21:19:58","date_gmt":"2016-09-21T05:19:58","guid":{"rendered":"http:\/\/wonghoi.humgar.com\/blog\/?p=300"},"modified":"2017-07-14T23:29:37","modified_gmt":"2017-07-15T07:29:37","slug":"matlab-gotchas-adding-whitespace-in-strcat","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2016\/09\/20\/matlab-gotchas-adding-whitespace-in-strcat\/","title":{"rendered":"MATLAB Gotchas: Adding whitespace in strcat()"},"content":{"rendered":"<p>strcat() is a very\u00a0handy function in\u00a0MATLAB that allows you to combine strings using a mixture of cellstr() and\u00a0char strings and it will auto-expand the char strings to match the cellstr() if necessary.<\/p>\n<p>However, by design intention, strcat() removes trailing white spaces by internally applying\u00a0deblank() to all <strong>char string<\/strong> inputs. It does NOT deblank cellstr() inputs. So if you want to combine date and time with a space, you have to use {&#8216; &#8216;} instead of &#8216; &#8216;:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"matlab\">date = '2000-01-01';\r\ntime = '00:00:01';\r\n&gt;&gt; strcat(date, ' ', time)  % The ' ' is ignored\r\nans =\r\n2000-01-0100:00:01\r\n&gt;&gt; strcat(date, {' '}, time)  % The ' ' is ignored\r\nans = \r\n    '2000-01-01 00:00:01'\r\n&gt;&gt;<\/pre>\n<p>I find this more confusing than helpful. Including myself and other users, we naturally resort to\u00a0processing line by line using cellfun() or other tricks just to get around the deblank()\u00a0problem without taking a second look at the documentation because<\/p>\n<ul>\n<li>rolling our own implementation is marginally as annoying as the deblank()<\/li>\n<li>we\u00a0expect\u00a0cellstr() to match the dimensions without auto-expanding. I naturally thought it would expand only if it&#8217;s a char string.<\/li>\n<\/ul>\n<p>Well, somebody asked <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/newsreader\/view_thread\/130915\">this question<\/a> on the newsgroup before, so obviously it&#8217;s not an intuitive design. It make sense to do it the way MATLAB designed strcat() because we need\u00a0some way to tell MATLAB whether I want my inputs deblanked or not.<\/p>\n<p>I think it&#8217;s more intuitive\u00a0to have MATLAB&#8217;s default strcat() not to deblank() char strings at all and have a strcat_deblanked() that deblanks the inputs before feeding into strcat().<\/p>\n<p>Unfortunately\u00a0this behavior is there for a long time, so it&#8217;s too late to change it without affecting compatibility. Might as well live with it, but this is one of the very few unnatural (or slightly illogical design choice)\u00a0of MATLAB\u00a0to keep in mind.<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_300\" class=\"pvc_stats all  \" data-element-id=\"300\" 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>strcat() is a very\u00a0handy function in\u00a0MATLAB that allows you to combine strings using a mixture of cellstr() and\u00a0char strings and it will auto-expand the char strings to match the cellstr() if necessary. However, by design intention, strcat() removes trailing white &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2016\/09\/20\/matlab-gotchas-adding-whitespace-in-strcat\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_300\" class=\"pvc_stats all  \" data-element-id=\"300\" 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-300","post","type-post","status-publish","format-standard","hentry","category-matlab"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/300","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=300"}],"version-history":[{"count":2,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/300\/revisions"}],"predecessor-version":[{"id":564,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/300\/revisions\/564"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}