MATLAB Functions Help Desk

sprintf

Purpose

Write formatted data to a string

Syntax

Description

s = sprintf(format,A,...) formats the data in matrix A (and in any additional matrix arguments) under control of the specified format string, and returns it in the MATLAB string variable s. sprintf is the same as fprintf except that it returns the data in a MATLAB string variable rather than writing it to a file.

The format string specifies notation, alignment, significant digits, field width, and other aspects of output format. It can contain ordinary alphanumeric characters; along with escape characters, conversion specifiers, and other characters, organized as shown below:

For more information see "Tables" and "References."

[s,errrmsg] = sprintf(format,A,...) returns an error message string errmsg if an error occurred or an empty matrix if an error did not occur.

Remarks

The sprintf function behaves like its ANSI C language sprintf() namesake with certain exceptions and extensions. These include:

  1. The following nonstandard subtype specifiers are supported for conversion specifiers %o, %u, %x, and %X.

    t

    The underlying C data type is a float rather than an unsigned integer.

    b

    The underlying C data type is a double rather than an unsigned integer.

  2. For example, to print a double-precision value in hexadecimal, use a format like '%bx'.
  3. sprintf is vectorized for the case when input matrix A is nonscalar. The format string is cycled through the elements of A (columnwise) until all the elements are used up. It is then cycled in a similar manner, without reinitializing, through any additional matrix arguments.

Tables

The following tables describe the nonalphanumeric characters found in format specification strings.

Escape Characters

Character

Description

\n

New line

\t

Horizontal tab

\b

Backspace

\r

Carriage return

\f

Form feed

\\

Backslash

\'' or ''

Single quotation mark

%%

Percent character

Conversion characters specify the notation of the output.

Conversion Specifiers

Specifier

Description

%c

Single character

%d

Decimal notation (signed)

%e

Exponential notation (using a lowercase e as in 3.1415e+00)

%E

Exponential notation (using an uppercase E as in 3.1415E+00)

%f

Fixed-point notation

%g

The more compact of %e or %f, as defined in [2]. Insignificant zeros do not print.

%G

Same as %g, but using an uppercase E

%o

Octal notation (unsigned)

%s

String of characters

%u

Decimal notation (unsigned)

%x

Hexadecimal notation (using lowercase letters a-f)

%X

Hexadecimal notation (using uppercase letters A-F)

Other characters can be inserted into the conversion specifier between the % and the conversion character.

Other Characters

Character

Description

Example

A minus sign (-)

Left-justifies the converted argument in its field.

%-5.2d

A plus sign (+)

Always prints a sign character (+ or -).

%+5.2d

Zero (0)

Pad with zeros rather than spaces.

%05.2d

Digits (field width)

A digit string specifying the minimum number of digits to be printed.

%6f

Digits (precision)

A digit string including a period (.) specifying the number of digits to be printed to the right of the decimal point.

%6.2f

Examples

Command

Result

sprintf('%0.5g',(1+sqrt(5))/2)

1.618

sprintf('%0.5g',1/eps)

4.5036e+15

sprintf('%15.5f',1/eps)

4503599627370496.00000

sprintf('%d',round(pi))

3

sprintf('%s','hello')

hello

sprintf('The array is %dx%d.',2,3)

The array is 2x3

sprintf('\n')

Line termination character on all platforms

See Also

int2str,num2str,sscanf

References

[1] Kernighan, B.W. and D.M. Ritchie, The C Programming Language, Second Edition, Prentice-Hall, Inc., 1988.

[2] ANSI specification X3.159-1989: "Programming Language C," ANSI, 1430 Broadway, New York, NY 10018.



[ Previous | Help Desk | Next ]