‘Custom Syntax for Formatted Strings with %s’\{SPRINTF}
Greg’s pick this week is Nice sprintf syntax using \ (left divide) by Will Fox.
A simple entry that exemplifies the extensibility of MATLAB.
Create a formatted character array with the following syntax
c = 'Hello %s, my favorite number is %.5f!'\{"Greg", pi}
c = 'Hello Greg, my favorite number is 3.14159!'
where the left argument is the format definition, and the right argument is a cell array of values applied to the format definition.
How is it done?
The author Will creates a new syntax for creating formatted character arrays in MATLAB where the left divide (MLDIVIDE) operator is used to apply the SPRINTF function.
MATLAB permits the overloading 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.
The code in this entry is very simple. It maps the input arguments to MLDIVIDE to the input arguments of SPRINTF.
Brilliance in simplicity.
Should I Learn a New Syntax?
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).
Injecting part of my philisophical view of coding, “consistency is key to readabilty”. In my experience, I spend much more 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.
I’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.
["Let's construct a string by including this syntax: " + '%s!'\{'format'} "Adding in some " + 'numbers: %f, %.0d, %-04.4g'\{exp(1), rand*10, pi} ]
ans = 2×1 string array "Let's construct a string by including this syntax: format!" "Adding in some numbers: 2.718282, 1e+00, 3.142"
A similar approach is taken for regular expressions in Javascript where a special compact syntax is applied for constant formatted regular expressions.
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.
Opportunities for Improvement
Using the ISCHAR and ISCELL functions when testing for the appropraite datatypes in the MLDIVIDE function will likely improve performance with no loss of readability. Currently the ISA function is used instead.
tic;isa('myArray', 'char'); toc
Elapsed time is 0.005289 seconds.
tic; ischar('myArray'); toc
Elapsed time is 0.000234 seconds.
Perhaps this can be extended for the new STRING datatype introduced in MATLAB R2016b. Or applied to regular expressions instead!
Should concise syntaxes like this be built into the MATLAB language?
Have you ever written overloaded methods for your classes? How else could this type of syntax be used?
Let us know here.
- Category:
- Picks
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.