Nice sprintf syntax using \ (left divide)

Overload the cell array '\' operator to have nice syntax for sprintf

現在この提出コンテンツをフォロー中です。

編集メモ: This file was selected as MATLAB Central Pick of the Week

If you are like me, you use sprintf extensively in matlab coding. When you learned python, you became jealous of the nice built-in printf interpolation operator in python (%).

This achieves the same in matlab, by overloading the cell array \ operator.

Old:
>> sprintf('%s %.2f', 'hello', pi)

New:
>> '%s %.2f' \ {'hello', pi}

See SPRINTF for documentation on the meaning of the format string

Notes:

To install: unzip and save this file "@cell/mldivide.m" on your path. (@cell applies this operator to the cell array class.)

I chose to overload the cell array function because the binary operators for the cell-array type are very sparsely defined. Furthermore, cell array brackets are natural here - the '\' operator has very high precedence, so if there is really any kind of math expression to be interpolated, it would likely have to be in some form of parenthesis anyway. The cell-array {} is a simple kind of parenthesis in this case. Finally this enables one single overloaded function to handle all variable types being interpolated.

An error is thrown if the arguments do not adhere to "fmt \ {args}", where fmt is a character string. No attempt is made to call to a builtin cell-array mldivide, as presently (Matlab 2012b) there is no such thing.

引用

Will Fox (2026). Nice sprintf syntax using \ (left divide) (https://jp.mathworks.com/matlabcentral/fileexchange/42088-nice-sprintf-syntax-using-left-divide), MATLAB Central File Exchange. に取得済み.

一般的な情報

MATLAB リリースの互換性

  • すべてのリリースと互換性あり

プラットフォームの互換性

  • Windows
  • macOS
  • Linux
バージョン 公開済み リリース ノート Action
1.2.0.0

Clean up MacOSX files from .zip

1.0.0.0