Skip to content

sprintf

Format data into a string.

s = sprintf(format, A, ...)
[s, errmsg] = sprintf(format, A, ...)

Returns a string created by formatting the input arguments according to the C-style format specification. Supports %d, %f, %e, %g, %s, %c, etc. with field-width and precision modifiers.

The optional second output errmsg contains a formatting error message (or empty string if formatting succeeded).

sprintf('%d items at $%.2f each', 12, 3.5) % '12 items at $3.50 each'
sprintf('%5d', [1 2 3]) % ' 1 2 3'
  • fprintf — Write formatted data to a file or the console.
  • num2str — Convert a number to a string.