Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

sprintf - Format data into string

Syntax

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

Description

str = sprintf(format, A, ...) applies the format to array A and any additional array arguments in column order, and returns the results to string str.

[str, errmsg] = sprintf(format, A, ...) returns an error message string when the operation is unsuccessful. Otherwise, errmsg is empty.

Inputs

format

String in single quotation marks that describes the format of the output fields. Can include combinations of the following:

  • Percent sign followed by a conversion character, such as '%s' for strings.

  • Operators that describe field width, precision, and other options.

  • Literal text to print.

  • Escape characters, including:

    ''

    Single quotation mark

    %%

    Percent character

    \\

    Backslash

    \b

    Backspace

    \f

    Form feed

    \n

    New line

    \r

    Carriage return

    \t

    Horizontal tab

    \xN

    Hexadecimal number, N

    \N

    Octal number, N

Conversion characters and optional operators appear in the following order (includes spaces for clarity):

The following table lists the available conversion characters and subtypes.

Value TypeConversionDetails

Integer, signed

%d or %i

Base 10 values

%ld or %li

64-bit base 10 values

Integer, unsigned

%u

Base 10

%o

Base 8 (octal)

%x

Base 16 (hexadecimal), lowercase letters af

%X

Same as %x, uppercase letters AF

%lu
%lo
%lx or %lX

64-bit values, base 10, 8, or 16

Floating-point number

%f

Fixed-point notation

%e

Exponential notation, such as 3.141593e+00

%E

Same as %e, but uppercase, such as 3.141593E+00

%g

The more compact of %e or %f, with no trailing zeros

%G

The more compact of %E or %f, with no trailing zeros

%bx or %bX
%bo
%bu

Double-precision hexadecimal, octal, or decimal value
Example: %bx prints pi as 400921fb54442d18

%tx or %tX
%to
%tu

Single-precision hexadecimal, octal, or decimal value
Example: %tx prints pi as 40490fdb

Characters

%c

Single character

%s

String of characters

Additional operators include:

  • Field width

    Minimum number of characters to print. Can be a number, or an asterisk (*) to refer to an argument in the input list. For example, the input list ('%12d', intmax) is equivalent to ('%*d', 12, intmax).

  • Precision

    For %f, %e, or %E:

    Number of digits to the right of the decimal point.
    Example: '%6.4f' prints pi as '3.1416'

    For %g or %G

    Number of significant digits.
    Example: '%6.4g' prints pi as ' 3.142'

    Can be a number, or an asterisk (*) to refer to an argument in the input list. For example, the input list ('%6.4f', pi) is equivalent to ('%*.*f', 6, 4, pi).

  • Flags

    Action

    Flag

    Example

    Left-justify.

    '–'

    %-5.2f

    Print sign character (+ or ).

    '+'

    %+5.2f

    Insert a space before the value.

    ' '

    % 5.2f

    Pad with zeros.

    '0'

    %05.2f

  • Identifier

    Order for processing inputs. Use the syntax n$, where n represents the position of the value in the input list.

    For example, '%3$s %2$s %1$s %2$s' prints inputs 'A', 'B', 'C' as follows: C B A B.

The following limitations apply to conversions:

  • Numeric conversions print only the real component of complex numbers.

  • If you apply an integer or string conversion to a numeric value that contains a fraction, MATLAB overrides the specified conversion, and uses %e.

  • If you apply a string conversion (%s) to integer values, MATLAB:

    • Issues a warning.

    • Converts values that correspond to valid character codes to characters. For example, '%s' converts [65 66 67] to ABC.

  • Different platforms display exponential notation (such as %e) with a different number of digits in the exponent.

    Platform

    Example

    Windows

    1.23e+004

    UNIX

    1.23e+04

  • Different platforms display negative zero (-0) differently.

     

    Conversion Character

    Platform

    %e or %E

    %f

    %g or %G

    Windows

    0.000000e+000

    0.000000

    0

    Others

    -0.000000e+00

    -0.000000

    -0

A

Numeric or character array.

Examples

Format floating-point numbers:

sprintf('%0.5f',1/eps)			% 4503599627370496.00000
sprintf('%0.5g',1/eps)			% 4.5036e+15
 

Explicitly convert double-precision values to integers:

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

Combine literal text with array values:

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

On a Windows system, convert PC-style exponential notation (three digits in the exponent) to UNIX-style notation (two digits):

a = sprintf('%e', 12345.678);
if ispc
   a = strrep(a, 'e+0', 'e+');
end

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.

See Also

fprintf | int2str | num2str | sscanf

How To

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS