Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

Numbered Argument Specification for Print Functions

Since MATLAB Release R2007a, you've been able to number the arguments for functions that format strings. With this feature,
you can refer to the values that correspond to the different arguments in a varying order. So what mischief can we get up
to?

 

Contents

Possible Uses

There are at least two cases I can think of where being able to refer to specific arguments by position can be useful.

  • Translation

This can be important to users writing code for broad use, where some of the output might need to be written in different
languages or the language the output is written in depends on the locale. Not all languages express certain ideas in the same
way, and this may necessitate using the arguments in a different order depending on the output language (sort of like cochon jaune in French vs. yellow pig in English).

  • Argument reuse

There are cases in which you might want to reuse an argument when writing out a string (perhaps a string of code or html).
Here's a case where we want to have a string and its value match each other; the radio button label is the actual value.

optionName = 'Case';
optionValue = 'Ignore';
s = sprintf(...
    '<input type=radio name="%1$s" value="%2$s"/>%2$s\n', ...
    optionName, optionValue)
s =
<input type=radio name="Case" value="Ignore"/>Ignore

References

Here are some MATLAB references for formatting strings.

  • Documentation for Formatting Strings - Note especially the section on restrictions for using the ordered identifiers.
  • R2007a Release Notes
  • See the help for these related functions: sprintf, fprintf, error, warning, assert

Other Uses?

Can you think of cases where you would take advantage of this? Post them here so we can see them!

Published with MATLAB® 7.5


  • print

评论

要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。