C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Formatting TextData is saved in the form of arrays. When we display the data, it comes in the form of an array, and that may not be as representable as always. So, there are some formatting operators available in the MATLAB to convert the data to text and format the output as per our requirements.
Example:>> % create a row vector using rand function >> r = rand(1,3); >> % combining normal text, special character, formatting operator in the output >> t = sprintf('Displaying 3 random numbers: \n 1)%f \n 2)%.2f \n 3)%12f',r); Output: >> t t = 'Displaying 3 random numbers: 1)0.957167 2)0.49 3)0.800280' Fields of the Formatting Operator in MATLABThe % percent sign always leads the formatting operator. A formatting operator can have a maximum of six fields-the conversion character, subtype, precision, field width, flags, and numeric identifier.
Syntax Example: Conversion CharacterThe conversion character is an only required field, and it specifies the notation of the output. The conversion character is denoted by a single alphabetical character and appears last in the format specifier.
Example: >>p=100; >>ch = 'cdeEfgGosuxX'; >>res = sprintf("%c\t\t%c \n%c\t\t%d \n%c\t\t%e \n%c\t\t%E"+... "\n%c\t\t%f \n%c\t\t%g \n%c\t\t%G \n%c\t\t%o"+... "\n%c\t\t%s\n%c\t\t%u\n%c\t\t%x\n%c\t\t%X?, ... ch(1),p,ch(2),p,+ch(3),p,ch(4),p,ch(5),p,ch(6),+... p,ch(7),p,ch(8),p,ch(9),p,ch(10),p,ch(11),p,ch(12),p); Output: >> res res = "c d d 100 e 1.000000e+02 E 1.000000E+02 f 100.000000 g 100 G 100 o 144 s d u 100 x 64 X 64" SubtypeThe subtype field is represented by a single alphabetic character that comes immediately before the conversion character. The conversion characters %o, %u, %x, and %X treat data input as integers, without the subtype field. So, use one of the following subtype specifiers to treat data input as floating-point values and convert them to octal, decimal, or hexadecimal representations.
Example: >> p = 100; >> res = sprintf('before using subtype:\t%u\nafter using subtype:\t%tu',p,p); Output: >> res res = 'before using subtype: 100 after using subtype: 1120403456' PrecisionThe precision field is denoted by a non-negative integer that immediately comes after a period (dot). It is used with %f, %e, and %E operators and indicates the number of digits to display to the right of the decimal point. Example: >> res = sprintf('output without and with precision field:\t%f\t%.2f\t%e\t%.2e',pi*30*ones(1,4)); Output: >> res res = 'output without and with precision field: 94.247780 94.25 9.424778e+01 9.42e+01' Field WidthThe field width is a non-negative integer and comes with or without decimal point precision. As the precision field specifies the number of digits after the decimal point, the field width specifies how many numbers of total characters should be displayed in the output. If the field width is higher than the number of characters in the input data, then by default, the output text is padded with space characters. Example: >> res = sprintf('output without and with field width:\t|%x|\t|%15x|\t|%f|\t|%15f|',303.3*ones(1,4)); Output: >> res res = 'output without and with field width: |3.033000e+02| | 3.033000e+02| |303.300000| | 303.300000|' FlagsThe flags field controls the additional formatting of the output. The characters used for the flags describe mainly the spacing, padding, and text alignment.
Example: -Right and left justify: >> sprintf('right-justify:|%+12s|\nleft-justify:|%-12.2f|',"flags",10); Output: >> ans ans = 'right-justify:| flags| left-justify:|10.00 |' Example: - Padding with space and zero: >> b = sprintf('padding with space: |%10.2f|\n padding with zero: |%010.2f|',20,20); Output: >> b b = 'padding with space: | 20.00| padding with zero: |0000020.00|' Example: -Pound Sign: >> a=sprintf('%#.0f',10.00); Output: >> a a = '10.' IdentifiersThe output functions such as sprintf prints the output in the same sequence as it takes input arguments. So, use the identifier to produce the output in a custom specified order. Identifiers are the integer values and come immediately after the % percent sign following by a $ dollar sign. Default orderExample: >> t1 = sprintf('%s %s %s','1st','2nd','3rd'); Output: >> t1 t1 = '1st 2nd 3rd' Custom order using identifierExample: >> t2 = sprintf('%3$s %1$s %2$s','1st','2nd','3rd'); Output: >> t2 t2 = '3rd 1st 2nd' Displaying Special Characters in the OutputSpecial characters are meant to serve particular purposes, so these cannot be used as ordinary text. To make special characters as part of the output text, use specific character sequences.
Rules for setting Field Width and PrecisionThe formatting operators follow specific rules for formatting the output text. And field width & precision define how a number should be displayed in the output. Let's examine with an illustrated example:
Field Width and Precision, Outside the Format SpecifierTo specify the field width and precision outside the format specifier, use an asterisk (*) in place of the field width or precision fields of the formatting operator. The place of the asterisk should match the place of the number specifying the fields in the input arguments. Lets' understand with an example: Example: >> t3 = sprintf('%*f %.*f %*.*f',10,123.456,3,10.2345,4,2,pi); Output: >> t3 t3 = ?123.456000 10.235 3.14? Explanation of the above example:
Specifying Identifiers in place of Field Width and PrecisionWe can specify numbered identifiers for field width and precision, and the value for the identifier comes from input arguments. Example: >> t4 = sprintf('%1$*4$f %2$.*5$f %3$*6$.*7$f',123.456, 12.36587, 3.145678, 10, 4, 4, 2); Output: >>t4 = '123.456000 12.3659 3.14' Lets' examine the above example:
Next Topicif...end statement
|