%% Formatting Text
% This example shows how to format data into text in various ways. It is not meant to be published as a standalone example, but rather to provide exampleobjects for the "Formatting Text" topic. That topic cannot be turned into an Example Manager example, as of Dec. 2016, because it contains tables that I wish to preserve for content improvement. (Stephen Doe, Jan. 2017)
%% Format Pi into Text

A = pi*ones(1,3);
txt = sprintf('%f | %.2f | %12f', A)

%
%% Format Pi into Text with Special Character

txt = sprintf('Displaying pi: \n %f \n %.2f \n %12f', A)

%
%% Format Numbers into Text

txt = sprintf('The price of %s on %d/%d/%d was $%.2f.',...
              'bread', 7, 1, 2006, 2.49)

%
%% Specify Input Value Arguments

txt = sprintf('%s = %d\n', 'A', 479, 'B', 352, 'C', 651)

%
%% Specify Input as Vector of Numbers

mvec = [77 65 84 76 65 66];
txt = sprintf('%s',char(mvec))

%
%% Specify Formatting Operators

A = pi*100*ones(1,5);
txt = sprintf(' %f \n %.2f \n %+.2f \n %12.2f \n %012.2f \n', A)

%
%% Display Numbers in Different Formats

A = 46*ones(1,4);
txt = sprintf('%d   %f   %e   %X', A)

%
%% Display Numbers with Different Precisions

txt = sprintf('%g   %.2g   %f   %.2f', pi*50*ones(1,4))

%
%% Display Numbers with Different Field Widths

txt = sprintf('|%e|%15e|%f|%15f|', pi*50*ones(1,4))

%
%% Display Text with Padding

txt = sprintf('%30s', 'Pad left with spaces')

%
%% Justify Text

txt = sprintf('right-justify: %12.2f\nleft-justify: %-12.2f',...
              12.3, 12.3)

%
%% Display Plus Sign for Numbers

txt = sprintf('no sign: %12.2f\nsign: %+12.2f',...
              12.3, 12.3)

%
%% Pad Display with Zeroes

txt = sprintf('Pad with spaces: %12.2f\nPad with zeroes: %012.2f',...
              5.2, 5.2)

%
%% Specify Width and Precision Outside Specifier

txt = sprintf('%*f   %.*f   %*.*f',...
              15,123.45678,...     
              3,16.42837,...       
              6,4,pi)

%
%% Mix Operators Inside and Outside Specifier

txt = sprintf('%*.2f', 5, 123.45678)

%
%% Obtain Width and Precision from Sequential List

txt = sprintf('%*f   %.*f   %*.*f',...
              15, 123.45678,...
              3,16.42837,...
              6,4,pi)

%
%% Specify Width and Precision with Numbered Ordering

txt = sprintf('%1$*4$f   %2$.*5$f   %3$*6$.*7$f',...
              123.45678, 16.42837, pi, 15, 3, 6, 4)

% Copyright 2012 The MathWorks, Inc.