1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Formatted Output (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Formatted Output (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Formatted Output (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="C_002dStyle-I_002fO-Functions.html" rel="up" title="C-Style I/O Functions">
<link href="Output-Conversion-for-Matrices.html" rel="next" title="Output Conversion for Matrices">
<link href="Line_002dOriented-Input.html" rel="prev" title="Line-Oriented Input">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Formatted-Output">
<div class="nav-panel">
<p>
Next: <a href="Output-Conversion-for-Matrices.html" accesskey="n" rel="next">Output Conversion for Matrices</a>, Previous: <a href="Line_002dOriented-Input.html" accesskey="p" rel="prev">Line-Oriented Input</a>, Up: <a href="C_002dStyle-I_002fO-Functions.html" accesskey="u" rel="up">C-Style I/O Functions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Formatted-Output-1"><span>14.2.4 Formatted Output<a class="copiable-link" href="#Formatted-Output-1"> ¶</a></span></h4>
<p>This section describes how to call <code class="code">printf</code> and related functions.
</p>
<p>The following functions are available for formatted output. They are
modeled after the C language functions of the same name, but they
interpret the format template differently in order to improve the
performance of printing vector and matrix values.
</p>
<p>Implementation Note: For compatibility with <small class="sc">MATLAB</small>, escape sequences in
the template string (e.g., <code class="code">"\n"</code> => newline) are
expanded even when the template string is defined with single quotes.
</p>
<a class="anchor" id="XREFprintf"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-printf"><span><strong class="def-name">printf</strong> <code class="def-code-arguments">(<var class="var">template</var>, …)</code><a class="copiable-link" href="#index-printf"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-printf-1"><span><code class="def-type"><var class="var">numbytes</var> =</code> <strong class="def-name">printf</strong> <code class="def-code-arguments">(…)</code><a class="copiable-link" href="#index-printf-1"> ¶</a></span></dt>
<dd><p>Print optional arguments under the control of the template string
<var class="var">template</var> to the stream <code class="code">stdout</code> and return the number of characters
printed.
</p>
<p>See the Formatted Output section of the GNU Octave manual for a complete
description of the syntax of the template string.
</p>
<p>The optional output <var class="var">numbytes</var> returns the number of bytes printed.
</p>
<p>Implementation Note: For compatibility with <small class="sc">MATLAB</small>, escape sequences in
the template string (e.g., <code class="code">"\n"</code> => newline) are
expanded even when the template string is defined with single quotes.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFfprintf">fprintf</a>, <a class="ref" href="#XREFsprintf">sprintf</a>, <a class="ref" href="Formatted-Input.html#XREFscanf">scanf</a>.
</p></dd></dl>
<a class="anchor" id="XREFfprintf"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-fprintf"><span><strong class="def-name">fprintf</strong> <code class="def-code-arguments">(<var class="var">fid</var>, <var class="var">template</var>, …)</code><a class="copiable-link" href="#index-fprintf"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-fprintf-1"><span><strong class="def-name">fprintf</strong> <code class="def-code-arguments">(<var class="var">template</var>, …)</code><a class="copiable-link" href="#index-fprintf-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-fprintf-2"><span><code class="def-type"><var class="var">numbytes</var> =</code> <strong class="def-name">fprintf</strong> <code class="def-code-arguments">(…)</code><a class="copiable-link" href="#index-fprintf-2"> ¶</a></span></dt>
<dd><p>This function is equivalent to <code class="code">printf</code>, except that the output is
written to the file descriptor <var class="var">fid</var> instead of <code class="code">stdout</code>.
</p>
<p>If <var class="var">fid</var> is omitted, the output is written to <code class="code">stdout</code> making the
function exactly equivalent to <code class="code">printf</code>.
</p>
<p>The optional output <var class="var">numbytes</var> returns the number of bytes written to the
file.
</p>
<p>Implementation Note: For compatibility with <small class="sc">MATLAB</small>, escape sequences in
the template string (e.g., <code class="code">"\n"</code> => newline) are
expanded even when the template string is defined with single quotes.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="Simple-Output.html#XREFfputs">fputs</a>, <a class="ref" href="Simple-File-I_002fO.html#XREFfdisp">fdisp</a>, <a class="ref" href="Binary-I_002fO.html#XREFfwrite">fwrite</a>, <a class="ref" href="Formatted-Input.html#XREFfscanf">fscanf</a>, <a class="ref" href="#XREFprintf">printf</a>, <a class="ref" href="#XREFsprintf">sprintf</a>, <a class="ref" href="Opening-and-Closing-Files.html#XREFfopen">fopen</a>.
</p></dd></dl>
<a class="anchor" id="XREFsprintf"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-sprintf"><span><code class="def-type"><var class="var">str</var> =</code> <strong class="def-name">sprintf</strong> <code class="def-code-arguments">(<var class="var">template</var>, …)</code><a class="copiable-link" href="#index-sprintf"> ¶</a></span></dt>
<dd><p>This is like <code class="code">printf</code>, except that the output is returned as a
string.
</p>
<p>Unlike the C library function, which requires you to provide a suitably
sized string as an argument, Octave’s <code class="code">sprintf</code> function returns the
string, automatically sized to hold all of the items converted.
</p>
<p>Implementation Note: For compatibility with <small class="sc">MATLAB</small>, escape sequences in
the template string (e.g., <code class="code">"\n"</code> => newline) are
expanded even when the template string is defined with single quotes.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFprintf">printf</a>, <a class="ref" href="#XREFfprintf">fprintf</a>, <a class="ref" href="Formatted-Input.html#XREFsscanf">sscanf</a>.
</p></dd></dl>
<p>The <code class="code">printf</code> function can be used to print any number of arguments.
The template string argument you supply in a call provides
information not only about the number of additional arguments, but also
about their types and what style should be used for printing them.
</p>
<p>Ordinary characters in the template string are simply written to the
output stream as-is, while <em class="dfn">conversion specifications</em> introduced by
a ‘<samp class="samp">%</samp>’ character in the template cause subsequent arguments to be
formatted and written to the output stream. For example,
<a class="index-entry-id" id="index-conversion-specifications-_0028printf_0029"></a>
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">pct = 37;
filename = "foo.txt";
printf ("Processed %d%% of '%s'.\nPlease be patient.\n",
pct, filename);
</pre></div></div>
<p>produces output like
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">Processed 37% of 'foo.txt'.
Please be patient.
</pre></div></div>
<p>This example shows the use of the ‘<samp class="samp">%d</samp>’ conversion to specify that a
scalar argument should be printed in decimal notation, the ‘<samp class="samp">%s</samp>’
conversion to specify printing of a string argument, and the ‘<samp class="samp">%%</samp>’
conversion to print a literal ‘<samp class="samp">%</samp>’ character.
</p>
<p>There are also conversions for printing an integer argument as an
unsigned value in octal, decimal, or hexadecimal radix (‘<samp class="samp">%o</samp>’,
‘<samp class="samp">%u</samp>’, or ‘<samp class="samp">%x</samp>’, respectively); or as a character value
(‘<samp class="samp">%c</samp>’).
</p>
<p>Floating-point numbers can be printed in normal, fixed-point notation
using the ‘<samp class="samp">%f</samp>’ conversion or in exponential notation using the
‘<samp class="samp">%e</samp>’ conversion. The ‘<samp class="samp">%g</samp>’ conversion uses either ‘<samp class="samp">%e</samp>’
or ‘<samp class="samp">%f</samp>’ format, depending on what is more appropriate for the
magnitude of the particular number.
</p>
<p>You can control formatting more precisely by writing <em class="dfn">modifiers</em>
between the ‘<samp class="samp">%</samp>’ and the character that indicates which conversion
to apply. These slightly alter the ordinary behavior of the conversion.
For example, most conversion specifications permit you to specify a
minimum field width and a flag indicating whether you want the result
left- or right-justified within the field.
</p>
<p>The specific flags and modifiers that are permitted and their
interpretation vary depending on the particular conversion. They’re all
described in more detail in the following sections.
</p>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Output-Conversion-for-Matrices.html">Output Conversion for Matrices</a>, Previous: <a href="Line_002dOriented-Input.html">Line-Oriented Input</a>, Up: <a href="C_002dStyle-I_002fO-Functions.html">C-Style I/O Functions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|