{"id":10235,"date":"2018-10-19T09:00:29","date_gmt":"2018-10-19T13:00:29","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=10235"},"modified":"2018-10-18T18:13:46","modified_gmt":"2018-10-18T22:13:46","slug":"custom-syntax-for-formatted-strings-with-ssprintf","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2018\/10\/19\/custom-syntax-for-formatted-strings-with-ssprintf\/","title":{"rendered":"&#8216;Custom Syntax for Formatted Strings with %s&#8217;\\{SPRINTF}"},"content":{"rendered":"<p><html xmlns:fo=\"http:\/\/www.w3.org\/1999\/XSL\/Format\" xmlns:xlink=\"http:\/\/www.w3.org\/1999\/xlink\"><\/p>\n<p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32620\">Greg&#8217;s<\/a> pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42088-nice-sprintf-syntax-using-left-divide\">Nice sprintf syntax using \\ (left divide)<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3230776-will-fox\">Will Fox<\/a>. <\/p>\n<p>A simple entry that exemplifies the extensibility of MATLAB.<\/p>\n<p>Create a formatted character array with the following syntax<\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">c = <span style=\"color:rgb(160, 32, 240);\">'Hello %s, my favorite number is %.5f!'<\/span>\\{<span >\"<\/span>Greg<span >\"<\/span>, pi}\r\n<\/pre>\n<pre class=\"output\" style=\"font-family:monospace;border:none;background-color:white;color:rgba(64, 64, 64, 1);\">c = \r\n'Hello Greg, my favorite number is 3.14159!'\r\n<\/pre>\n<p>where the left argument is the format definition, and the right argument is a cell array of values applied to the format definition.<\/p>\n<h3>How is it done?<\/h3>\n<p>The author Will creates a new syntax for creating formatted character arrays in MATLAB where the left divide (<a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/matlab\/ref\/mldivide.html\">MLDIVIDE<\/a>) operator is used to apply the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/matlab\/ref\/sprintf.html\">SPRINTF<\/a> function. <\/p>\n<p>MATLAB permits the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/matlab\/matlab_oop\/implementing-operators-for-your-class.html\">overloading<\/a> of many operators. Will took advantage of an operator that is not available for cell arrays, and added it to the Cell class of datatypes in MATLAB. <\/p>\n<p>The code in this entry is very simple. It maps the input arguments to MLDIVIDE to the input arguments of SPRINTF.<\/p>\n<p>Brilliance in simplicity.<\/p>\n<h3>Should I Learn a New Syntax?<\/h3>\n<p>Adding new syntaxes to your code can make things harder in the long term if they are not used consistently (at least within a particular project). <\/p>\n<p>Injecting part of my philisophical view of coding, &#8220;consistency is key to readabilty&#8221;. In my experience, I spend <em>much more<\/em> time reading code than writing it, so applying consistent coding patterns is a key practice for me, even if no one else will see the code. If the code written is useful then I will likely need to maintain and enhance it in the future. I apply the same ideas when constructing Simulink and Stateflow models. <\/p>\n<p>I&#8217;m a fan of this very crisp syntax as more consise syntaxes can (but not always) reduce cognitive load once you have learned to interpret them. <\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">[<span >\"<\/span>Let's construct a string by including this syntax: <span >\"<\/span> + <span style=\"color:rgb(160, 32, 240);\">'%s!'<\/span>\\{<span style=\"color:rgb(160, 32, 240);\">'format'<\/span>}\r\n    <span >\"<\/span>Adding in <span style=\"color:rgb(160, 32, 240);\">some<\/span> <span style=\"color:rgb(160, 32, 240);\">\"<\/span> <span style=\"color:rgb(160, 32, 240);\">+<\/span> <span style=\"color:rgb(160, 32, 240);\">'numbers: %f, %.0d, %-04.4g'\\{exp(1), rand*10, pi}<\/span>\r\n]\r\n<\/pre>\n<pre class=\"output\" style=\"font-family:monospace;border:none;background-color:white;color:rgba(64, 64, 64, 1);\">ans = <em>2&times;1 string array<\/em>\r\n    \"Let's construct a string by including this syntax: format!\"\r\n    \"Adding in some numbers: 2.718282, 1e+00, 3.142\"\r\n\r\n<\/pre>\n<p>A <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Regular_Expressions\">similar<\/a> approach is taken for regular expressions in Javascript where a special compact syntax is applied for constant formatted regular expressions. <\/p>\n<p>But, be careful! This is not a standard syntax and it does not appear in the MATLAB Documentation. This will hinder new coders who come onto the project and are not familiar with this syntax or the ability to overload MATLAB operators. <\/p>\n<h3>Opportunities for Improvement<\/h3>\n<p>Using the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/matlab\/ref\/ischar.html\">ISCHAR<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/matlab\/ref\/iscell.html\">ISCELL<\/a> functions when testing for the appropraite datatypes in the MLDIVIDE function will likely improve performance with no loss of readability. Currently the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/matlab\/ref\/isa.html\">ISA<\/a> function is used instead. <\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">tic;isa(<span style=\"color:rgb(160, 32, 240);\">'myArray'<\/span>, <span style=\"color:rgb(160, 32, 240);\">'char'<\/span>); toc\r\n<\/pre>\n<pre class=\"output\" style=\"font-family:monospace;border:none;background-color:white;color:rgba(64, 64, 64, 1);\">Elapsed time is 0.005289 seconds.\r\n<\/pre>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">tic; ischar(<span style=\"color:rgb(160, 32, 240);\">'myArray'<\/span>); toc\r\n<\/pre>\n<pre class=\"output\" style=\"font-family:monospace;border:none;background-color:white;color:rgba(64, 64, 64, 1);\">Elapsed time is 0.000234 seconds.\r\n<\/pre>\n<p>Perhaps this can be extended for the new <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/matlab\/ref\/string.html\">STRING<\/a> datatype introduced in MATLAB R2016b. Or applied to <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/matlab\/ref\/regexp.html\">regular expressions<\/a> instead! <\/p>\n<h3>Should concise syntaxes like this be built into the MATLAB language?<\/h3>\n<p>Have you ever written overloaded methods for your classes? How else could this type of syntax be used?<\/p>\n<p>Let us know <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=10235#respond\">here<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\nGreg&#8217;s pick this week is Nice sprintf syntax using \\ (left divide) by Will Fox.<br \/>\nA simple entry that exemplifies the extensibility of MATLAB.<br \/>\nCreate a formatted character array with the&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2018\/10\/19\/custom-syntax-for-formatted-strings-with-ssprintf\/\">read more >><\/a><\/p>\n","protected":false},"author":36,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10235"}],"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\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=10235"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10235\/revisions"}],"predecessor-version":[{"id":10249,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10235\/revisions\/10249"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=10235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=10235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=10235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}