How can I distribute a single sprintf input to multiple formatting operators?

3 ビュー (過去 30 日間)
Fletcher Young
Fletcher Young 2025 年 8 月 20 日
コメント済み: Stephen23 2025 年 8 月 20 日
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?

採用された回答

Steven Lord
Steven Lord 2025 年 8 月 20 日
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)
S = 'foo 42 foo'
  2 件のコメント
Fletcher Young
Fletcher Young 2025 年 8 月 20 日
This is what I was looking for. Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by