printf
Displays the string str to the Command window or Console prompt.
Syntax
R = printf(str)
R = printf(format_string, var1, ...)
Inputs
- str
- Text to be displayed.
- format_string
- The format_string can consist of text to be displayed and any number of placeholders for one or more variables.
- var1, ...
- Zero, one, or more variable names can be specified after the format_string. For proper format control, care should be exercised in matching the variable type with the placeholder type in the format_string.
Outputs
- R
- The number of characters printed.
Examples
Simple printf example to display a string:
printf('hello world')
hello world
Complex printf example showing a number of format controls:
test = 'test';
printf('\n')
printf('print %-8s %-22s **%s**\n', 'string', ' ', test)
printf('print %-8s %-22s **%10s**\n', 'string', 'minimum size', test)
printf('print %-8s %-22s **%-10s**\n', 'string', 'string left justified', test)
printf('\n') % print blank line between sections
numtest = 1234568;
printf('\n')
printf('print %-8s %-22s **%d**\n', 'number', ' ', numtest)
printf('print %-8s %-22s **%10d**\n', 'number', 'minimum size', numtest)
printf('print %-8s %-22s **%-10d**\n', 'number', 'left justified', numtest)
printf('print %-8s %-22s **%010d**\n', 'number', 'zero fill', numtest)
printf('\n') % print blank line between sections
format('long')
floatest = 120 + pi
printf('\n')
printf('print %-8s %-22s **%f**\n', 'float', ' ', floatest)
printf('print %-8s %-22s **%14f**\n', 'float', 'minimum width', floatest)
printf('print %-8s %-22s **%-14f**\n', 'float', 'left justified', floatest)
printf('print %-8s %-22s **%014f**\n', 'float', 'zero filled', floatest)
printf('print %-8s %-22s **%014.3f**\n', 'float', '3 decimal restriction', floatest)
printf('print %-8s %-22s **%014.8f**\n', 'float', '8 decimal expansion', floatest)
print string **test**
print string minimum size ** test**
print string string left justified **test **
print number **1234568**
print number minimum size ** 1234568**
print number left justified **1234568 **
print number zero fill **0001234568**
floatest = 123.14159265
print float **123.141593**
print float minimum width ** 123.141593**
print float left justified **123.141593 **
print float zero filled **0000123.141593**
print float 3 decimal restriction **0000000123.142**
print float 8 decimal expansion **00123.14159265**
Comments
The format_string follows the conventions for the C language printf format string.
Text is written as entered into the format string. Variables are written as placeholders, identified by % (percent sign), and occur in the format string. Different variable types requre different placeholder characters, as follows: %d for integer variables. %s for string variables. %f for floating point variables %e prints scientific notation for integers and floating point variables. %g prints integer or floating point variables using scientific notation if necessary. %% prints a single '%' (percent sign) character. \n represents a newline. Text following the newline will begin output on the next line. Modifiers can be specified for each placeholder type to control the width of characters printed, left or right justification, space or zero filled, and decimal numbers displayed. A selection of modifiers is discussed below. %d Modifiers %10d Adding a number after the % controls the minimum width of the printed variable. The example prints a decimal number right-justified, space-filled in a 10 character column. %-10 Adding minus sign '-' after the % prints a decimal number left-justified. The example prints left-justified within a 10 character column. %010d Adding a lead zero after the % print a decimal number with lead zeros. The example prints right-justified, zero-filled within a 10 character column. %s Modifiers %10s Adding a number after the % controls the minimum width of the printed string. The example prints a string right-justified, space-filled in a 10 character column. %-10s Adding minus sin '-' after the % prints a string left-justified. The example prints left-justified within a 10 character column. %f Modifiers %10f Adding a number after the % controls the minimum width of the printed variable. The example prints a variable right-justified, space-filled in a 10 character column. %-10f Adding minus sign '-' after the % prints a string left-justified. The example prints left-justified within a 10 character column. %010f Adding a lead zero after the % prints a variable with lead zeros. The example prints right-justified, zero-filled within a 10 character column. %10.3f Adding a decimal after the % prints can restrict or expand the default number of decimals printed. The example prints right-justified, space-filled within a 10 character column with 3 decimals.