## Sprintfing to the finish

October 06, 2011 at 06:23 PM | categories: basic matlab | View Comments

## Contents

## 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.

1/3 0.25 10.1

ans = 0.3333 ans = 0.2500 ans = 10.1000

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

format long 1/3 0.25 10.1 % to reset the format to the default, simply call format format

ans = 0.333333333333333 ans = 0.250000000000000 ans = 10.100000000000000

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 = 0.33

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:

sprintf('%1.3g',1/3) % Note the first zero is not significant sprintf('%1.3g',4/3) % Note we know this to lower precision, because the first digit is % significant.

ans = 0.333 ans = 1.33

## 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 = -1.00 1.00

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

ans = -1.00 +1.00

## Scientific notation

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

eps

ans = 2.2204e-016

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

```
sprintf('%1.12f',eps)
```

ans = 0.000000000000

Instead, we use scientific notation

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

a = 2.22e-016

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.

## Summary

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.

'done' % categories: basic matlab

ans = done