{"id":3438,"date":"2019-09-09T09:47:40","date_gmt":"2019-09-09T14:47:40","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=3438"},"modified":"2019-09-04T11:54:12","modified_gmt":"2019-09-04T16:54:12","slug":"compose-yourself","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2019\/09\/09\/compose-yourself\/","title":{"rendered":"Compose Yourself!"},"content":{"rendered":"\r\n<div class=\"content\"><!--introduction--><p>Even if we crunch numbers a lot, there are plenty of times, for example, when reporting, that we need to mix numbers into text.  For a very long time, we've been able to create a character vector, and more recently, a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/string.html\"><tt>string<\/tt><\/a>, with functions like <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/sprintf.html\"><tt>sprintf<\/tt><\/a>.  Look <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/characters-and-strings.html\">here<\/a> for more information on character arrays and strings.<\/p><p>That's fine if we want to create one textual entity, but what if I need an array of them?  I can use <tt>sprintf<\/tt> in a loop, or I can use a newer function, <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/compose.html\"><tt>compose<\/tt><\/a>.  Let me show you a bit how they each work.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#5d5ae9a1-1d37-4e9f-bbbc-c5844a131f17\">A Single Numeric Input<\/a><\/li><li><a href=\"#b7c8b21c-dd78-48fc-8267-5c1e70818089\">Multiple Numeric Inputs<\/a><\/li><li><a href=\"#5dc602c8-d564-402a-aa34-c08bae2209c3\">Handling Holes<\/a><\/li><li><a href=\"#63119b2c-22c3-4830-951c-c9366ee1371b\">Your Turn<\/a><\/li><\/ul><\/div><h4>A Single Numeric Input<a name=\"5d5ae9a1-1d37-4e9f-bbbc-c5844a131f17\"><\/a><\/h4><p>Let's create some numeric values to use as possible inputs.<\/p><pre class=\"codeinput\">nums = [exp(1) pi]\r\n<\/pre><pre class=\"codeoutput\">nums =\r\n    2.7183    3.1416\r\n<\/pre><p>Here's character output from <tt>sprintf<\/tt>:<\/p><pre class=\"codeinput\">cs1 = sprintf(<span class=\"string\">'%d'<\/span>, nums(2))\r\n<\/pre><pre class=\"codeoutput\">cs1 =\r\n    '3.141593e+00'\r\n<\/pre><p>And here's the equivalent <tt>string<\/tt> output:<\/p><pre class=\"codeinput\">ss1 = sprintf(<span class=\"string\">\"%d\"<\/span>, nums(2))\r\n<\/pre><pre class=\"codeoutput\">ss1 = \r\n    \"3.141593e+00\"\r\n<\/pre><p>Now let's try <tt>compose<\/tt>:<\/p><pre class=\"codeinput\">cc1 = compose(<span class=\"string\">'%d'<\/span>, nums(2))\r\n<\/pre><pre class=\"codeoutput\">cc1 =\r\n  1&times;1 cell array\r\n    {'3.141593e+00'}\r\n<\/pre><pre class=\"codeinput\">sc1 = compose(<span class=\"string\">\"%d\"<\/span>, nums(2))\r\n<span class=\"comment\">% What do we have in our workspace?<\/span>\r\nwhos\r\n<\/pre><pre class=\"codeoutput\">sc1 = \r\n    \"3.141593e+00\"\r\n  Name      Size            Bytes  Class     Attributes\r\n\r\n  ans       1x4                 8  char                \r\n  cc1       1x1               136  cell                \r\n  ccm1      1x2               272  cell                \r\n  cs1       1x12               24  char                \r\n  csm1      1x26               52  char                \r\n  nums      1x2                16  double              \r\n  sc1       1x1               174  string              \r\n  scm1      1x2               252  string              \r\n  ss1       1x1               174  string              \r\n  ssm1      1x1               206  string              \r\n  tc        1x1               120  cell                \r\n  tmpc      1x2               228  cell                \r\n  tmps      1x2                 4  char                \r\n  ts        1x2                 4  char                \r\n\r\n<\/pre><p>What you may notice is that both <tt>sprintf<\/tt> and <tt>compose<\/tt> give the same output for the string version.  However, for the character output, we get one character array using <tt>sprintf<\/tt>, but that same array embedded in a cell using <tt>compose<\/tt>.<\/p><pre class=\"codeinput\">clearvars <span class=\"string\">-except<\/span> <span class=\"string\">nums<\/span>\r\n<\/pre><h4>Multiple Numeric Inputs<a name=\"b7c8b21c-dd78-48fc-8267-5c1e70818089\"><\/a><\/h4><p>Now let's see what happens if we are formatting output with more than a single numeric input.<\/p><pre class=\"codeinput\">csm1 = sprintf(<span class=\"string\">'%d '<\/span>,nums)\r\nssm1 = sprintf(<span class=\"string\">\"%d \"<\/span>,nums)\r\nccm1 = compose(<span class=\"string\">'%d'<\/span>,nums)\r\nscm1 = compose(<span class=\"string\">\"%d\"<\/span>,nums)\r\n<\/pre><pre class=\"codeoutput\">csm1 =\r\n    '2.718282e+00 3.141593e+00 '\r\nssm1 = \r\n    \"2.718282e+00 3.141593e+00 \"\r\nccm1 =\r\n  1&times;2 cell array\r\n    {'2.718282e+00'}    {'3.141593e+00'}\r\nscm1 = \r\n  1&times;2 string array\r\n    \"2.718282e+00\"    \"3.141593e+00\"\r\n<\/pre><pre class=\"codeinput\">whos\r\n<\/pre><pre class=\"codeoutput\">  Name      Size            Bytes  Class     Attributes\r\n\r\n  ccm1      1x2               272  cell                \r\n  csm1      1x26               52  char                \r\n  nums      1x2                16  double              \r\n  scm1      1x2               252  string              \r\n  ssm1      1x1               206  string              \r\n\r\n<\/pre><p>What we see is that we get exactly the same types of output as before. But for the <tt>string<\/tt> version with <tt>compose<\/tt>, we get output matching the dimensions of the input, in this case, <tt>1x2<\/tt>, whereas with the <tt>sprintf<\/tt> version, we get a single array for all of the cases. <tt>sprintf<\/tt> always produces a single array as output and recycles the hole (the formatter) while there is more data to format. <tt>compose<\/tt> returns arrays of text.<\/p><h4>Handling Holes<a name=\"5dc602c8-d564-402a-aa34-c08bae2209c3\"><\/a><\/h4><p>Again, <tt>sprintf<\/tt> produces one output, regardless of the dimensions of the input.<\/p><pre class=\"codeinput\">tmps = sprintf(<span class=\"string\">'%d'<\/span>,[1 2])\r\ntmpc = compose(<span class=\"string\">'%d'<\/span>,[1 2])\r\n<\/pre><pre class=\"codeoutput\">tmps =\r\n    '12'\r\ntmpc =\r\n  1&times;2 cell array\r\n    {'1'}    {'2'}\r\n<\/pre><p>Also, <tt>sprintf<\/tt> truncates a hole if you don&#8217;t provide data. <tt>compose<\/tt> leaves it there in case you want to fill it with a subsequent call:<\/p><pre class=\"codeinput\">ts = sprintf(<span class=\"string\">'\\n %s'<\/span>)\r\n\r\ntc = compose(<span class=\"string\">'\\n %s'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">ts =\r\n    '\r\n      '\r\ntc =\r\n  1&times;1 cell array\r\n    {'&#8629; %s'}\r\n<\/pre><h4>Your Turn<a name=\"63119b2c-22c3-4830-951c-c9366ee1371b\"><\/a><\/h4><p>Do you need to format text\/numbers?  What techniques do you use to get the results you want?  Let us know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=3438#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_932c3385a4184f589175dbf27f346bfd() {\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='932c3385a4184f589175dbf27f346bfd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 932c3385a4184f589175dbf27f346bfd';\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        copyright = 'Copyright 2019 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_932c3385a4184f589175dbf27f346bfd()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2019a<br><\/p><\/div><!--\r\n932c3385a4184f589175dbf27f346bfd ##### SOURCE BEGIN #####\r\n%% Compose Yourself!\r\n% Even if we crunch numbers a lot, there are plenty of times, for example,\r\n% when reporting, that we need to mix numbers into text.  For a very long\r\n% time, we've been able to create a character vector, and more recently, a\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/string.html |string|>, with\r\n% functions like <https:\/\/www.mathworks.com\/help\/matlab\/ref\/sprintf.html\r\n% |sprintf|>.  Look\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/characters-and-strings.html here>\r\n% for more information on character arrays and strings.\r\n%\r\n% That's fine if we want to create one textual entity, but what if I need\r\n% an array of them?  I can use |sprintf| in a loop, or I can use a newer\r\n% function, <https:\/\/www.mathworks.com\/help\/matlab\/ref\/compose.html\r\n% |compose|>.  Let me show you a bit how they each work.\r\n%% A Single Numeric Input\r\n% Let's create some numeric values to use as possible inputs.\r\nnums = [exp(1) pi]\r\n%%\r\n% Here's character output from |sprintf|:\r\ncs1 = sprintf('%d', nums(2))\r\n\r\n%%\r\n% And here's the equivalent |string| output:\r\nss1 = sprintf(\"%d\", nums(2))\r\n%%\r\n% Now let's try |compose|:\r\ncc1 = compose('%d', nums(2))\r\n%%\r\nsc1 = compose(\"%d\", nums(2))\r\n% What do we have in our workspace?\r\nwhos\r\n%%\r\n% What you may notice is that both |sprintf| and |compose| give the same\r\n% output for the string version.  However, for the character output, we get\r\n% one character array using |sprintf|, but that same array embedded in a\r\n% cell using |compose|.\r\n%\r\nclearvars -except nums\r\n%% Multiple Numeric Inputs\r\n% Now let's see what happens if we are formatting output with more than a\r\n% single numeric input.\r\ncsm1 = sprintf('%d ',nums)\r\nssm1 = sprintf(\"%d \",nums)\r\nccm1 = compose('%d',nums)\r\nscm1 = compose(\"%d\",nums)\r\n%%\r\nwhos\r\n%%\r\n% What we see is that we get exactly the same types of output as before.\r\n% But for the |string| version with |compose|, we get output matching the\r\n% dimensions of the input, in this case, |1x2|, whereas with the |sprintf|\r\n% version, we get a single array for all of the cases. |sprintf| always\r\n% produces a single array as output and recycles the hole (the formatter)\r\n% while there is more data to format. |compose| returns arrays of text.\r\n\r\n%% Handling Holes\r\n% Again, |sprintf| produces one output, regardless of the dimensions of the\r\n% input.\r\ntmps = sprintf('%d',[1 2])\r\ntmpc = compose('%d',[1 2])\r\n%%\r\n% Also, |sprintf| truncates a hole if you don\u00e2\u20ac&#x2122;t provide data. |compose|\r\n% leaves it there in case you want to fill it with a subsequent call:\r\nts = sprintf('\\n %s')\r\n\r\ntc = compose('\\n %s')\r\n%% Your Turn\r\n% Do you need to format text\/numbers?  What techniques do you use to get\r\n% the results you want?  Let us know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=3438#respond here>.\r\n\r\n##### SOURCE END ##### 932c3385a4184f589175dbf27f346bfd\r\n-->","protected":false},"excerpt":{"rendered":"<!--introduction--><p>Even if we crunch numbers a lot, there are plenty of times, for example, when reporting, that we need to mix numbers into text.  For a very long time, we've been able to create a character vector, and more recently, a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/string.html\"><tt>string<\/tt><\/a>, with functions like <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/sprintf.html\"><tt>sprintf<\/tt><\/a>.  Look <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/characters-and-strings.html\">here<\/a> for more information on character arrays and strings.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2019\/09\/09\/compose-yourself\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3438"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=3438"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3438\/revisions"}],"predecessor-version":[{"id":3442,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3438\/revisions\/3442"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=3438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=3438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=3438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}