Sprintfing to the finish

| categories: basic matlab | View Comments



Sprintfing to the finish

John Kitchin

There are many times where you need to control the format of how variables are displayed in Matlab. A crude way to control this is through the format command. By default, Matlab shows decimal numbers on a fixed scale with 5 decimals.

ans =


ans =


ans =


To show more decimals, you can issue a 'format long' command

format long

% to reset the format to the default, simply call format
ans =


ans =


ans =


But suppose you want to print results with the correct number of significant figures, which will vary. For that, you need the sprintf command. The sprintf command takes a string, with a format specifier in it, and replaces the format specifier with a numeric value. A typical specifier for a decimal number is %w.df, where w is the width of the field, and d is the number of decimals, or precision. The field width is the minimum number of characters to print. See the documentation for sprintf (doc sprintf) for much more detail. To print with only two decimal places, consider these two examples

n = 1/3;
a = sprintf('%1.2f', n)
a =


You can embed the format strings into a string

a = sprintf('The value of 1/3 to two decimal places is %1.2f', n)
% Note that sprintf returns a string, that can be used in other
% places.
a =

The value of 1/3 to two decimal places is 0.33

You can have more than one format string in a string, and then you have to have more than one value to pass to each format string. The values get passed in order.

sprintf('value 1 = %1.2f and value 2 = %1.4f', 1/3,1/6)
ans =

value 1 = 0.33 and value 2 = 0.1667

You can reuse a format string several times for multiple values

sprintf('The answers are: %1.2f  ', [1/3 1/6 1/9])
ans =

The answers are: 0.33  The answers are: 0.17  The answers are: 0.11  

Note that the whole string is repeated for each element in the array. To get a better output, we might put a carriage return after each line like this

sprintf('The answers are: %1.2f\n', [1/3 1/6 1/9])
ans =

The answers are: 0.33
The answers are: 0.17
The answers are: 0.11

Significant digit specification

If you know how many significant digits you want, we use the %g format string. To get 1/3 to 3 significant digits:

% Note the first zero is not significant
% Note we know this to lower precision, because the first digit is
% significant.
ans =


ans =


modifier flags to format strings

If you want to print a +- sign with the data, compare these two outputs:

sprintf('%1.2f\n',[-1 1])
% the decimals do not align, making it a little harder to read
ans =


sprintf('%+1.2f\n',[-1 1])
% here the decimals align, and it is easier to read.
ans =


Scientific notation

the eps command gives you the smallest possible spacing between float numbers in matlab.

ans =


As a float, this number is practically zero to many decimal places. As a decimal, it is impractical to view.

ans =


Instead, we use scientific notation

a = sprintf('%1.2e',eps)
a =


you can combine the string output with this syntax.

['the answer is: '  a]
ans =

the answer is: 2.22e-016

Or you can use fprintf

fprintf('the answer is: %1.2e\n',eps)
% What is the difference between fprintf and sprintf? fprintf prints
% directly to the screen, and does not output any result, avoiding the
the answer is: 2.22e-016
ans = ....
in the display window. So if you don't need the string output as a
variable, the results are cleaner with sprintf.


That is how simple it is to control the format of how your data is displayed. There are more options than shown here, but this is the majority of formatting I find necessary most of the time.


% categories: basic matlab
ans =


blog comments powered by Disqus