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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Converting Numerical Data to Strings (GNU Octave (version 6.2.0))</title>
<meta name="description" content="Converting Numerical Data to Strings (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Converting Numerical Data to Strings (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<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="Creating-Strings.html" rel="up" title="Creating Strings">
<link href="Comparing-Strings.html" rel="next" title="Comparing Strings">
<link href="Concatenating-Strings.html" rel="prev" title="Concatenating Strings">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<span id="Converting-Numerical-Data-to-Strings"></span><div class="header">
<p>
Previous: <a href="Concatenating-Strings.html" accesskey="p" rel="prev">Concatenating Strings</a>, Up: <a href="Creating-Strings.html" accesskey="u" rel="up">Creating Strings</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>
<span id="Converting-Numerical-Data-to-Strings-1"></span><h4 class="subsection">5.3.2 Converting Numerical Data to Strings</h4>
<p>Apart from the string concatenation functions (see <a href="Concatenating-Strings.html">Concatenating Strings</a>)
which cast numerical data to the corresponding UTF-8 encoded characters, there
are several functions that format numerical data as strings. <code>mat2str</code>
and <code>num2str</code> convert real or complex matrices, while <code>int2str</code>
converts integer matrices. <code>int2str</code> takes the real part of complex
values and round fractional values to integer. A more flexible way to format
numerical data as strings is the <code>sprintf</code> function
(see <a href="Formatted-Output.html">Formatted Output</a>, <a href="Formatted-Output.html#XREFsprintf">sprintf</a>).
</p>
<span id="XREFmat2str"></span><dl>
<dt id="index-mat2str">: <em><var>s</var> =</em> <strong>mat2str</strong> <em>(<var>x</var>, <var>n</var>)</em></dt>
<dt id="index-mat2str-1">: <em><var>s</var> =</em> <strong>mat2str</strong> <em>(<var>x</var>, <var>n</var>, "class")</em></dt>
<dd><p>Format real, complex, and logical matrices as strings.
</p>
<p>The returned string may be used to reconstruct the original matrix by using
the <code>eval</code> function.
</p>
<p>The precision of the values is given by <var>n</var>. If <var>n</var> is a scalar
then both real and imaginary parts of the matrix are printed to the same
precision. Otherwise <code><var>n</var>(1)</code> defines the precision of the real
part and <code><var>n</var>(2)</code> defines the precision of the imaginary part.
The default for <var>n</var> is 15.
</p>
<p>If the argument <code>"class"</code> is given then the class of <var>x</var> is
included in the string in such a way that <code>eval</code> will result in the
construction of a matrix of the same class.
</p>
<div class="example">
<pre class="example">mat2str ([ -1/3 + i/7; 1/3 - i/7 ], [4 2])
⇒ "[-0.3333+0.14i;0.3333-0.14i]"
mat2str ([ -1/3 +i/7; 1/3 -i/7 ], [4 2])
⇒ "[-0.3333+0i 0+0.14i;0.3333+0i -0-0.14i]"
mat2str (int16 ([1 -1]), "class")
⇒ "int16([1 -1])"
mat2str (logical (eye (2)))
⇒ "[true false;false true]"
isequal (x, eval (mat2str (x)))
⇒ 1
</pre></div>
<p><strong>See also:</strong> <a href="Formatted-Output.html#XREFsprintf">sprintf</a>, <a href="#XREFnum2str">num2str</a>, <a href="#XREFint2str">int2str</a>.
</p></dd></dl>
<span id="XREFnum2str"></span><dl>
<dt id="index-num2str">: <em></em> <strong>num2str</strong> <em>(<var>x</var>)</em></dt>
<dt id="index-num2str-1">: <em></em> <strong>num2str</strong> <em>(<var>x</var>, <var>precision</var>)</em></dt>
<dt id="index-num2str-2">: <em></em> <strong>num2str</strong> <em>(<var>x</var>, <var>format</var>)</em></dt>
<dd><p>Convert a number (or array) to a string (or a character array).
</p>
<p>The optional second argument may either give the number of significant
digits (<var>precision</var>) to be used in the output or a format template
string (<var>format</var>) as in <code>sprintf</code> (see <a href="Formatted-Output.html">Formatted Output</a>).
<code>num2str</code> can also process complex numbers.
</p>
<p>Examples:
</p>
<div class="example">
<pre class="example">num2str (123.456)
⇒ 123.456
num2str (123.456, 4)
⇒ 123.5
s = num2str ([1, 1.34; 3, 3.56], "%5.1f")
⇒ s =
1.0 1.3
3.0 3.6
whos s
⇒ Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
s 2x8 16 char
Total is 16 elements using 16 bytes
num2str (1.234 + 27.3i)
⇒ 1.234+27.3i
</pre></div>
<p>The <code>num2str</code> function is not very flexible. For better control
over the results, use <code>sprintf</code> (see <a href="Formatted-Output.html">Formatted Output</a>).
</p>
<p>Programming Notes:
</p>
<p>For <small>MATLAB</small> compatibility, leading spaces are stripped before returning
the string.
</p>
<p>Integers larger than <code>flintmax</code> may not be displayed correctly.
</p>
<p>For complex <var>x</var>, the format string may only contain one output
conversion specification and nothing else. Otherwise, results will be
unpredictable.
</p>
<p>Any optional <var>format</var> specified by the programmer is used without
modification. This is in contrast to <small>MATLAB</small> which tampers with the
<var>format</var> based on internal heuristics.
</p>
<p><strong>See also:</strong> <a href="Formatted-Output.html#XREFsprintf">sprintf</a>, <a href="#XREFint2str">int2str</a>, <a href="#XREFmat2str">mat2str</a>.
</p></dd></dl>
<span id="XREFint2str"></span><dl>
<dt id="index-int2str">: <em></em> <strong>int2str</strong> <em>(<var>n</var>)</em></dt>
<dd><p>Convert an integer (or array of integers) to a string (or a character
array).
</p>
<div class="example">
<pre class="example">int2str (123)
⇒ 123
s = int2str ([1, 2, 3; 4, 5, 6])
⇒ s =
1 2 3
4 5 6
whos s
⇒ Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
s 2x7 14 char
Total is 14 elements using 14 bytes
</pre></div>
<p>This function is not very flexible. For better control over the
results, use <code>sprintf</code> (see <a href="Formatted-Output.html">Formatted Output</a>).
</p>
<p>Programming Notes:
</p>
<p>Non-integers are rounded to integers before display. Only the real part
of complex numbers is displayed.
</p>
<p><strong>See also:</strong> <a href="Formatted-Output.html#XREFsprintf">sprintf</a>, <a href="#XREFnum2str">num2str</a>, <a href="#XREFmat2str">mat2str</a>.
</p></dd></dl>
<hr>
<div class="header">
<p>
Previous: <a href="Concatenating-Strings.html" accesskey="p" rel="prev">Concatenating Strings</a>, Up: <a href="Creating-Strings.html" accesskey="u" rel="up">Creating Strings</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>
|