% sprintf returns a string.
% This string can be used to annotate plots using the text() function.
s = sprintf( 'x=%f', pi )

% fprintf writes directly to console (or into files).
% for fprintf you usually want to add the line break '\n':

% '%f' formats floating point numbers:
fprintf( 'x=%f\n', pi )
% The '%f' formatting string can be anywhere in the string:
fprintf( 'x=%fms\n', pi )
% There can be arbitrary many '%' formatting strings:
fprintf( 'x=%fms, y=%fkHz\n', pi, 2*pi )
% The '%' itself is generated by '%%':
fprintf( 'x=%f%%\n', pi )
% A point followed by a number sets the number of digits after the point:
fprintf( 'x=%.2fms\n', pi )
% The numbers are appropriately rounded:
fprintf( 'x=%.3fms\n', pi )
% A number right before the point sets the width of the generated output:
fprintf( 'x=%10.3fms\n', pi )
% '%e' also formats floating point numbers but forces to write in
% exponential style:
fprintf( 'x=%e\n', pi )
% again, a point and number set the number of digits after the point.
fprintf( 'x=%.1e\n', pi )
% '%g% formats the floating point number to a given number of valid digits
% (default is 5):
fprintf( 'x=%g\n', pi )
% The number of valid digits is not the number of digits after the point:
fprintf( 'x=%.2g\n', pi )
fprintf( 'x=%.2g\n', 10.123 )
fprintf( 'x=%.2g\n', 18765.123 )
fprintf( 'x=%.5g\n', 18765.123 )

% '%d' formats integers:
fprintf( 'x=%d\n', 5 )
% the number defines the width of the output:
fprintf( 'x=%3d\n', 5 )
% precedig the width with a '0' fills up the space with leading zeros:
fprintf( 'x=%03d\n', 5 )

% '%s' formats a string:
fprintf( 'x=%s\n', 'hallo' )
% ... aligned to the right:
fprintf( 'x=%10s\n', 'hallo' )
% ... unless the width is negative:
fprintf( 'x=%-10s!\n', 'hallo' )
