Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 181

octave-doc-3.6.4-3.mga4.noarch.rpm

<html lang="en">
<head>
<title>Conversion of Numerical Data to Strings - GNU Octave</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Octave">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Creating-Strings.html#Creating-Strings" title="Creating Strings">
<link rel="prev" href="Concatenating-Strings.html#Concatenating-Strings" title="Concatenating Strings">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
  pre.display { font-family:inherit }
  pre.format  { font-family:inherit }
  pre.smalldisplay { font-family:inherit; font-size:smaller }
  pre.smallformat  { font-family:inherit; font-size:smaller }
  pre.smallexample { font-size:smaller }
  pre.smalllisp    { font-size:smaller }
  span.sc    { font-variant:small-caps }
  span.roman { font-family:serif; font-weight:normal; } 
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
--></style>
</head>
<body>
<div class="node">
<a name="Conversion-of-Numerical-Data-to-Strings"></a>
<p>
Previous:&nbsp;<a rel="previous" accesskey="p" href="Concatenating-Strings.html#Concatenating-Strings">Concatenating Strings</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Creating-Strings.html#Creating-Strings">Creating Strings</a>
<hr>
</div>

<h4 class="subsection">5.3.2 Conversion of Numerical Data to Strings</h4>

<p>Apart from the string concatenation functions (see <a href="Concatenating-Strings.html#Concatenating-Strings">Concatenating Strings</a>)
which cast numerical data to the corresponding ASCII 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">Formatted Output</a>,
<a href="doc_002dsprintf.html#doc_002dsprintf">doc-sprintf</a>).

<!-- mat2str scripts/strings/mat2str.m -->
   <p><a name="doc_002dmat2str"></a>

<div class="defun">
&mdash; Function File: <var>s</var> = <b>mat2str</b> (<var>x, n</var>)<var><a name="index-mat2str-343"></a></var><br>
&mdash; Function File: <var>s</var> = <b>mat2str</b> (<var>x, n, "class"</var>)<var><a name="index-mat2str-344"></a></var><br>
<blockquote><p>Format real, complex, and logical matrices as strings.  The
returned string may be used to reconstruct the original matrix by using
the <code>eval</code> function.

        <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 <var>n</var><code>(1)</code> defines the
precision of the real part and <var>n</var><code>(2)</code> defines the
precision of the imaginary part.  The default for <var>n</var> is 15.

        <p>If the argument "class" 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.

     <pre class="example">          mat2str ([ -1/3 + i/7; 1/3 - i/7 ], [4 2])
               &rArr; "[-0.3333+0.14i;0.3333-0.14i]"
          
          mat2str ([ -1/3 +i/7; 1/3 -i/7 ], [4 2])
               &rArr; "[-0.3333+0i 0+0.14i;0.3333+0i -0-0.14i]"
          
          mat2str (int16([1 -1]), "class")
               &rArr; "int16([1 -1])"
          
          mat2str (logical (eye (2)))
               &rArr; "[true false;false true]"
          
          isequal (x, eval (mat2str (x)))
               &rArr; 1
</pre>
        <!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
     <!-- A simple blank line produces the correct behavior. -->
     <!-- @sp 1 -->
     <p class="noindent"><strong>See also:</strong> <a href="doc_002dsprintf.html#doc_002dsprintf">sprintf</a>, <a href="doc_002dnum2str.html#doc_002dnum2str">num2str</a>, <a href="doc_002dint2str.html#doc_002dint2str">int2str</a>. 
</p></blockquote></div>

<!-- num2str scripts/general/num2str.m -->
   <p><a name="doc_002dnum2str"></a>

<div class="defun">
&mdash; Function File:  <b>num2str</b> (<var>x</var>)<var><a name="index-num2str-345"></a></var><br>
&mdash; Function File:  <b>num2str</b> (<var>x, precision</var>)<var><a name="index-num2str-346"></a></var><br>
&mdash; Function File:  <b>num2str</b> (<var>x, format</var>)<var><a name="index-num2str-347"></a></var><br>
<blockquote><p>Convert a number (or array) to a string (or a character array).  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">Formatted Output</a>).  <code>num2str</code> can also handle complex numbers.  For
example:

     <pre class="example">          num2str (123.456)
               &rArr; "123.46"
          
          num2str (123.456, 4)
               &rArr; "123.5"
          
          s = num2str ([1, 1.34; 3, 3.56], "%5.1f")
               &rArr; s =
                  1.0  1.3
                  3.0  3.6
          whos s
               &rArr;
                Attr Name        Size                     Bytes  Class
                ==== ====        ====                     =====  =====
                     s           2x8                         16  char
          
          num2str (1.234 + 27.3i)
               &rArr; "1.234+27.3i"
</pre>
        <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">Formatted Output</a>). 
Note that for complex <var>x</var>, the format string may only contain one
output conversion specification and nothing else.  Otherwise, you
will get unpredictable results. 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002dsprintf.html#doc_002dsprintf">sprintf</a>, <a href="doc_002dint2str.html#doc_002dint2str">int2str</a>, <a href="doc_002dmat2str.html#doc_002dmat2str">mat2str</a>. 
</p></blockquote></div>

<!-- int2str scripts/general/int2str.m -->
   <p><a name="doc_002dint2str"></a>

<div class="defun">
&mdash; Function File:  <b>int2str</b> (<var>n</var>)<var><a name="index-int2str-348"></a></var><br>
<blockquote><p>Convert an integer (or array of integers) to a string (or a character
array).

     <pre class="example">          int2str (123)
               &rArr; "123"
          
          s = int2str ([1, 2, 3; 4, 5, 6])
               &rArr; s =
                  1  2  3
                  4  5  6
          
          whos s
               &rArr; s =
                Attr Name        Size                     Bytes  Class
                ==== ====        ====                     =====  =====
                     s           2x7                         14  char
</pre>
        <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">Formatted Output</a>). 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002dsprintf.html#doc_002dsprintf">sprintf</a>, <a href="doc_002dnum2str.html#doc_002dnum2str">num2str</a>, <a href="doc_002dmat2str.html#doc_002dmat2str">mat2str</a>. 
</p></blockquote></div>

   </body></html>