How can I distribute a single sprintf input to multiple formatting operators?
33 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Fletcher Young
il 20 Ago 2025 alle 14:07
Commentato: Stephen23
il 20 Ago 2025 alle 18:28
Currently, I have a setup like: sprintf('%s %s','foo','foo'). I would ideally like something more concise, like: sprintf('%s %s','foo'), with an output of 'foo foo'. Is there a clean way to do this?
0 Commenti
Risposta accettata
Steven Lord
il 20 Ago 2025 alle 17:30
You can specify which of the data inputs you want each "section" of the format specifier input you want to use by adding a number and $ between the % and the formatting character. In the example below, the first data input 'foo' is used twice, once in the first section of the format specifier and once in the third section. Between those two, the second section uses the second data input 42.
S = sprintf('%1$s %2$d %1$s', 'foo', 42)
2 Commenti
Stephen23
il 20 Ago 2025 alle 18:28
See under Format Spec -> Optional Operators -> Identifier:
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!