Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 3e60ff9d4d6f58c8fbd17208f14089fa > files > 369

octave-doc-3.2.3-3mdv2010.0.i586.rpm

<html lang="en">
<head>
<title>Simple File I/O - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Basic-Input-and-Output.html#Basic-Input-and-Output" title="Basic Input and Output">
<link rel="prev" href="Terminal-Input.html#Terminal-Input" title="Terminal Input">
<link rel="next" href="Rational-Approximations.html#Rational-Approximations" title="Rational Approximations">
<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="Simple-File-I%2fO"></a>
<a name="Simple-File-I_002fO"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Rational-Approximations.html#Rational-Approximations">Rational Approximations</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Terminal-Input.html#Terminal-Input">Terminal Input</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Basic-Input-and-Output.html#Basic-Input-and-Output">Basic Input and Output</a>
<hr>
</div>

<h4 class="subsection">14.1.3 Simple File I/O</h4>

<p><a name="index-saving-data-727"></a><a name="index-loading-data-728"></a>The <code>save</code> and <code>load</code> commands allow data to be written to and
read from disk files in various formats.  The default format of files
written by the <code>save</code> command can be controlled using the functions
<code>default_save_options</code> and <code>save_precision</code>.

   <p>As an example the following code creates a 3-by-3 matrix and saves it
to the file &lsquo;<samp><span class="samp">myfile.mat</span></samp>&rsquo;.

<pre class="example">     A = [ 1:3; 4:6; 7:9 ];
     save myfile.mat A
</pre>
   <p>Once one or more variables have been saved to a file, they can be
read into memory using the <code>load</code> command.

<pre class="example">     load myfile.mat
     A
          -| A =
          -|
          -|    1   2   3
          -|    4   5   6
          -|    7   8   9
</pre>
   <!-- load-save.cc -->
   <p><a name="doc_002dsave"></a>

<div class="defun">
&mdash; Command: <b>save</b><var> file<a name="index-save-729"></a></var><br>
&mdash; Command: <b>save</b><var> options file<a name="index-save-730"></a></var><br>
&mdash; Command: <b>save</b><var> options file v1 v2 <small class="dots">...</small><a name="index-save-731"></a></var><br>
&mdash; Command: <b>save</b><var> options file -struct STRUCT f1 f2 <small class="dots">...</small><a name="index-save-732"></a></var><br>
<blockquote><p>Save the named variables <var>v1</var>, <var>v2</var>, <small class="dots">...</small>, in the file
<var>file</var>.  The special filename &lsquo;<samp><span class="samp">-</span></samp>&rsquo; may be used to write
output to the terminal.  If no variable names are listed, Octave saves
all the variables in the current scope.  Otherwise, full variable names or
pattern syntax can be used to specify the variables to save. 
If the <code>-struct</code> modifier is used, fields <var>f1</var> <var>f2</var> <small class="dots">...</small>
of the scalar structure <var>STRUCT</var> are saved as if they were variables
with corresponding names. 
Valid options for the <code>save</code> command are listed in the following table. 
Options that modify the output format override the format specified by
<code>default_save_options</code>.

        <p>If save is invoked using the functional form

     <pre class="example">          save ("-option1", ..., "file", "v1", ...)
</pre>
        <p class="noindent">then the <var>options</var>, <var>file</var>, and variable name arguments
(<var>v1</var>, <small class="dots">...</small>) must be specified as character strings.

          <dl>
<dt><code>-ascii</code><dd>Save a single matrix in a text file without header or any other information.

          <br><dt><code>-binary</code><dd>Save the data in Octave's binary data format.

          <br><dt><code>-float-binary</code><dd>Save the data in Octave's binary data format but only using single
precision.  Only use this format if you know that all the
values to be saved can be represented in single precision.

          <br><dt><code>-hdf5</code><dd>Save the data in HDF5 format. 
(HDF5 is a free, portable binary format developed by the National
Center for Supercomputing Applications at the University of Illinois.)

          <br><dt><code>-float-hdf5</code><dd>Save the data in HDF5 format but only using single precision. 
Only use this format if you know that all the
values to be saved can be represented in single precision.

          <br><dt><code>-V7</code><dt><code>-v7</code><dt><code>-7</code><dt><code>-mat7-binary</code><dd>Save the data in <span class="sc">matlab</span>'s v7 binary data format.

          <br><dt><code>-V6</code><dt><code>-v6</code><dt><code>-6</code><dt><code>-mat</code><dt><code>-mat-binary</code><dd>Save the data in <span class="sc">matlab</span>'s v6 binary data format.

          <br><dt><code>-V4</code><dt><code>-v4</code><dt><code>-4</code><dt><code>-mat4-binary</code><dd>Save the data in the binary format written by <span class="sc">matlab</span> version 4.

          <br><dt><code>-text</code><dd>Save the data in Octave's text data format.  (default).

          <br><dt><code>-zip</code><dt><code>-z</code><dd>Use the gzip algorithm to compress the file.  This works equally on files that
are compressed with gzip outside of octave, and gzip can equally be used to
convert the files for backward compatibility. 
</dl>

        <p>The list of variables to save may use wildcard patterns containing
the following special characters:
          <dl>
<dt><code>?</code><dd>Match any single character.

          <br><dt><code>*</code><dd>Match zero or more characters.

          <br><dt><code>[ </code><var>list</var><code> ]</code><dd>Match the list of characters specified by <var>list</var>.  If the first
character is <code>!</code> or <code>^</code>, match all characters except those
specified by <var>list</var>.  For example, the pattern <code>[a-zA-Z]</code> will
match all lower and upper case alphabetic characters.

          <p>Wildcards may also be used in the field name specifications when using
the <code>-struct</code> modifier (but not in the struct name itself).

        </dl>

        <p>Except when using the <span class="sc">matlab</span> binary data file format or the
&lsquo;<samp><span class="samp">-ascii</span></samp>&rsquo; format, saving global
variables also saves the global status of the variable.  If the variable
is restored at a later time using &lsquo;<samp><span class="samp">load</span></samp>&rsquo;, it will be restored as a
global variable.

        <p>The command

     <pre class="example">          save -binary data a b*
</pre>
        <p class="noindent">saves the variable &lsquo;<samp><span class="samp">a</span></samp>&rsquo; and all variables beginning with &lsquo;<samp><span class="samp">b</span></samp>&rsquo; to
the file <samp><span class="file">data</span></samp> in Octave's binary format. 
<!-- 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_002dload.html#doc_002dload">load</a>, <a href="doc_002ddefault_005fsave_005foptions.html#doc_002ddefault_005fsave_005foptions">default_save_options</a>, <a href="doc_002ddlmread.html#doc_002ddlmread">dlmread</a>, <a href="doc_002dcsvread.html#doc_002dcsvread">csvread</a>, <a href="doc_002dfread.html#doc_002dfread">fread</a>. 
</p></blockquote></div>

<!-- load-save.cc -->
   <p><a name="doc_002dload"></a>

<div class="defun">
&mdash; Command: <b>load</b><var> file<a name="index-load-733"></a></var><br>
&mdash; Command: <b>load</b><var> options file<a name="index-load-734"></a></var><br>
&mdash; Command: <b>load</b><var> options file v1 v2 <small class="dots">...</small><a name="index-load-735"></a></var><br>
&mdash; Command: <b>S</b><var> = load</var>(<var>"options", "file", "v1", "v2", <small class="dots">...</small></var>)<var><a name="index-S-736"></a></var><br>
<blockquote><p>Load the named variables <var>v1</var>, <var>v2</var>, <small class="dots">...</small>, from the file
<var>file</var>.  If no variables are specified then all variables found in the
file will be loaded.  As with <code>save</code>, the list of variables to extract
can be full names or use a pattern syntax.  The format of the file is
automatically detected but may be overridden by supplying the appropriate
option.

        <p>If load is invoked using the functional form

     <pre class="example">          load ("-option1", ..., "file", "v1", ...)
</pre>
        <p class="noindent">then the <var>options</var>, <var>file</var>, and variable name arguments
(<var>v1</var>, <small class="dots">...</small>) must be specified as character strings.

        <p>If a variable that is not marked as global is loaded from a file when a
global symbol with the same name already exists, it is loaded in the
global symbol table.  Also, if a variable is marked as global in a file
and a local symbol exists, the local symbol is moved to the global
symbol table and given the value from the file.

        <p>If invoked with a single output argument, Octave returns data instead
of inserting variables in the symbol table.  If the data file contains
only numbers (TAB- or space-delimited columns), a matrix of values is
returned.  Otherwise, <code>load</code> returns a structure with members
 corresponding to the names of the variables in the file.

        <p>The <code>load</code> command can read data stored in Octave's text and
binary formats, and <span class="sc">matlab</span>'s binary format.  If compiled with zlib
support, it can also load gzip-compressed files.  It will automatically
detect the type of file and do conversion from different floating point
formats (currently only IEEE big and little endian, though other formats
may be added in the future).

        <p>Valid options for <code>load</code> are listed in the following table.

          <dl>
<dt><code>-force</code><dd>This option is accepted for backward compatibility but is ignored. 
Octave now overwrites variables currently in memory with
those of the same name found in the file.

          <br><dt><code>-ascii</code><dd>Force Octave to assume the file contains columns of numbers in text format
without any header or other information.  Data in the file will be loaded
as a single numeric matrix with the name of the variable derived from the
name of the file.

          <br><dt><code>-binary</code><dd>Force Octave to assume the file is in Octave's binary format.

          <br><dt><code>-hdf5</code><dd>Force Octave to assume the file is in HDF5 format. 
(HDF5 is a free, portable binary format developed by the National
Center for Supercomputing Applications at the University of Illinois.) 
Note that Octave can read HDF5 files not created by itself, but may
skip some datasets in formats that it cannot support.

          <br><dt><code>-import</code><dd>This option is accepted for backward compatibility but is ignored. 
Octave can now support multi-dimensional HDF data and automatically
modifies variable names if they are invalid Octave identifiers.

          <br><dt><code>-mat</code><dt><code>-mat-binary</code><dt><code>-6</code><dt><code>-v6</code><dt><code>-7</code><dt><code>-v7</code><dd>Force Octave to assume the file is in <span class="sc">matlab</span>'s version 6 or 7 binary
format.

          <br><dt><code>-mat4-binary</code><dt><code>-4</code><dt><code>-v4</code><dt><code>-V4</code><dd>Force Octave to assume the file is in the binary format written by
<span class="sc">matlab</span> version 4.

          <br><dt><code>-text</code><dd>Force Octave to assume the file is in Octave's text format. 
</dl>
        <!-- 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_002dsave.html#doc_002dsave">save</a>, <a href="doc_002ddlmwrite.html#doc_002ddlmwrite">dlmwrite</a>, <a href="doc_002dcsvwrite.html#doc_002dcsvwrite">csvwrite</a>, <a href="doc_002dfwrite.html#doc_002dfwrite">fwrite</a>. 
</p></blockquote></div>

   <p>There are three functions that modify the behavior of <code>save</code>.

<!-- load-save.cc -->
   <p><a name="doc_002ddefault_005fsave_005foptions"></a>

<div class="defun">
&mdash; Built-in Function: <var>val</var> = <b>default_save_options</b> ()<var><a name="index-default_005fsave_005foptions-737"></a></var><br>
&mdash; Built-in Function: <var>old_val</var> = <b>default_save_options</b> (<var>new_val</var>)<var><a name="index-default_005fsave_005foptions-738"></a></var><br>
<blockquote><p>Query or set the internal variable that specifies the default options
for the <code>save</code> command, and defines the default format. 
Typical values include <code>"-ascii"</code>, <code>"-text -zip"</code>. 
The default value is <code>-text</code>. 
<!-- 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_002dsave.html#doc_002dsave">save</a>. 
</p></blockquote></div>

<!-- ls-oct-ascii.cc -->
   <p><a name="doc_002dsave_005fprecision"></a>

<div class="defun">
&mdash; Built-in Function: <var>val</var> = <b>save_precision</b> ()<var><a name="index-save_005fprecision-739"></a></var><br>
&mdash; Built-in Function: <var>old_val</var> = <b>save_precision</b> (<var>new_val</var>)<var><a name="index-save_005fprecision-740"></a></var><br>
<blockquote><p>Query or set the internal variable that specifies the number of
digits to keep when saving data in text format. 
</p></blockquote></div>

<!-- load-save.cc -->
   <p><a name="doc_002dsave_005fheader_005fformat_005fstring"></a>

<div class="defun">
&mdash; Built-in Function: <var>val</var> = <b>save_header_format_string</b> ()<var><a name="index-save_005fheader_005fformat_005fstring-741"></a></var><br>
&mdash; Built-in Function: <var>old_val</var> = <b>save_header_format_string</b> (<var>new_val</var>)<var><a name="index-save_005fheader_005fformat_005fstring-742"></a></var><br>
<blockquote><p>Query or set the internal variable that specifies the format
string used for the comment line written at the beginning of
text-format data files saved by Octave.  The format string is
passed to <code>strftime</code> and should begin with the character
&lsquo;<samp><span class="samp">#</span></samp>&rsquo; and contain no newline characters.  If the value of
<code>save_header_format_string</code> is the empty string,
the header comment is omitted from text-format data files.  The
default value is

     <!-- Set example in small font to prevent overfull line -->
     <pre class="smallexample">          "# Created by Octave VERSION, %a %b %d %H:%M:%S %Y %Z &lt;USER@HOST&gt;"
</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_002dstrftime.html#doc_002dstrftime">strftime</a>, <a href="doc_002dsave.html#doc_002dsave">save</a>. 
</p></blockquote></div>

<!-- sysdep.cc -->
   <p><a name="doc_002dnative_005ffloat_005fformat"></a>

<div class="defun">
&mdash; Built-in Function:  <b>native_float_format</b> ()<var><a name="index-native_005ffloat_005fformat-743"></a></var><br>
<blockquote><p>Return the native floating point format as a string
</p></blockquote></div>

   <p>It is possible to write data to a file in a similar way to the
<code>disp</code> function for writing data to the screen.  The <code>fdisp</code>
works just like <code>disp</code> except its first argument is a file pointer
as created by <code>fopen</code>.  As an example, the following code writes
to data &lsquo;<samp><span class="samp">myfile.txt</span></samp>&rsquo;.

<pre class="example">     fid = fopen ("myfile.txt", "w");
     fdisp (fid, "3/8 is ");
     fdisp (fid, 3/8);
     fclose (fid);
</pre>
   <p class="noindent">See <a href="Opening-and-Closing-Files.html#Opening-and-Closing-Files">Opening and Closing Files</a>, for details on how to use <code>fopen</code>
and <code>fclose</code>.

<!-- pr-output.cc -->
   <p><a name="doc_002dfdisp"></a>

<div class="defun">
&mdash; Built-in Function:  <b>fdisp</b> (<var>fid, x</var>)<var><a name="index-fdisp-744"></a></var><br>
<blockquote><p>Display the value of <var>x</var> on the stream <var>fid</var>.  For example,

     <pre class="example">          fdisp (stdout, "The value of pi is:"), fdisp (stdout, pi)
          
               -| the value of pi is:
               -| 3.1416
</pre>
        <p class="noindent">Note that the output from <code>fdisp</code> always ends with a newline. 
<!-- 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_002ddisp.html#doc_002ddisp">disp</a>. 
</p></blockquote></div>

   <p>Octave can also read and write matrices text files such as comma
separated lists.

<!-- ./io/dlmwrite.m -->
   <p><a name="doc_002ddlmwrite"></a>

<div class="defun">
&mdash; Function File:  <b>dlmwrite</b> (<var>file, a</var>)<var><a name="index-dlmwrite-745"></a></var><br>
&mdash; Function File:  <b>dlmwrite</b> (<var>file, a, delim, r, c</var>)<var><a name="index-dlmwrite-746"></a></var><br>
&mdash; Function File:  <b>dlmwrite</b> (<var>file, a, key, val <small class="dots">...</small></var>)<var><a name="index-dlmwrite-747"></a></var><br>
&mdash; Function File:  <b>dlmwrite</b> (<var>file, a, "-append", <small class="dots">...</small></var>)<var><a name="index-dlmwrite-748"></a></var><br>
<blockquote><p>Write the matrix <var>a</var> to the named file using delimiters.

        <p>The parameter <var>delim</var> specifies the delimiter to use to separate
values on a row.

        <p>The value of <var>r</var> specifies the number of delimiter-only lines to
add to the start of the file.

        <p>The value of <var>c</var> specifies the number of delimiters to prepend to
each line of data.

        <p>If the argument <code>"-append"</code> is given, append to the end of the
<var>file</var>.

        <p>In addition, the following keyword value pairs may appear at the end
of the argument list:
          <dl>
<dt><code>"append"</code><dd>Either &lsquo;<samp><span class="samp">"on"</span></samp>&rsquo; or &lsquo;<samp><span class="samp">"off"</span></samp>&rsquo;.  See &lsquo;<samp><span class="samp">"-append"</span></samp>&rsquo; above.

          <br><dt><code>"delimiter"</code><dd>See <var>delim</var> above.

          <br><dt><code>"newline"</code><dd>The character(s) to use to separate each row.  Three special cases
exist for this option.  &lsquo;<samp><span class="samp">"unix"</span></samp>&rsquo; is changed into '\n',
&lsquo;<samp><span class="samp">"pc"</span></samp>&rsquo; is changed into '\r\n', and &lsquo;<samp><span class="samp">"mac"</span></samp>&rsquo; is changed
into '\r'.  Other values for this option are kept as is.

          <br><dt><code>"roffset"</code><dd>See <var>r</var> above.

          <br><dt><code>"coffset"</code><dd>See <var>c</var> above.

          <br><dt><code>"precision"</code><dd>The precision to use when writing the file.  It can either be a
format string (as used by fprintf) or a number of significant digits. 
</dl>

     <pre class="example">          dlmwrite ("file.csv", reshape (1:16, 4, 4));
</pre>
        <pre class="example">          dlmwrite ("file.tex", a, "delimiter", "&amp;", "newline", "\\n")
</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_002ddlmread.html#doc_002ddlmread">dlmread</a>, <a href="doc_002dcsvread.html#doc_002dcsvread">csvread</a>, <a href="doc_002dcsvwrite.html#doc_002dcsvwrite">csvwrite</a>. 
</p></blockquote></div>

<!-- ./DLD-FUNCTIONS/dlmread.cc -->
   <p><a name="doc_002ddlmread"></a>

<div class="defun">
&mdash; Loadable Function: <var>data</var> = <b>dlmread</b> (<var>file</var>)<var><a name="index-dlmread-749"></a></var><br>
&mdash; Loadable Function: <var>data</var> = <b>dlmread</b> (<var>file, sep</var>)<var><a name="index-dlmread-750"></a></var><br>
&mdash; Loadable Function: <var>data</var> = <b>dlmread</b> (<var>file, sep, r0, c0</var>)<var><a name="index-dlmread-751"></a></var><br>
&mdash; Loadable Function: <var>data</var> = <b>dlmread</b> (<var>file, sep, range</var>)<var><a name="index-dlmread-752"></a></var><br>
<blockquote><p>Read the matrix <var>data</var> from a text file.  If not defined the separator
between fields is determined from the file itself.  Otherwise the
separation character is defined by <var>sep</var>.

        <p>Given two scalar arguments <var>r0</var> and <var>c0</var>, these define the starting
row and column of the data to be read.  These values are indexed from zero,
such that the first row corresponds to an index of zero.

        <p>The <var>range</var> parameter must be a 4 element vector containing the upper
left and lower right corner <code>[</code><var>R0</var><code>,</code><var>C0</var><code>,</code><var>R1</var><code>,</code><var>C1</var><code>]</code> or
a spreadsheet style range such as 'A2..Q15'.  The lowest index value is zero. 
</p></blockquote></div>

<!-- ./io/csvwrite.m -->
   <p><a name="doc_002dcsvwrite"></a>

<div class="defun">
&mdash; Function File: <var>x</var> = <b>csvwrite</b> (<var>filename, x</var>)<var><a name="index-csvwrite-753"></a></var><br>
<blockquote><p>Write the matrix <var>x</var> to a file.

        <p>This function is equivalent to
     <pre class="example">          dlmwrite (<var>filename</var>, <var>x</var>, ",", ...)
</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_002ddlmread.html#doc_002ddlmread">dlmread</a>, <a href="doc_002ddlmwrite.html#doc_002ddlmwrite">dlmwrite</a>, <a href="doc_002dcsvread.html#doc_002dcsvread">csvread</a>. 
</p></blockquote></div>

<!-- ./io/csvread.m -->
   <p><a name="doc_002dcsvread"></a>

<div class="defun">
&mdash; Function File: <var>x</var> = <b>csvread</b> (<var>filename</var>)<var><a name="index-csvread-754"></a></var><br>
<blockquote><p>Read the matrix <var>x</var> from a file.

        <p>This function is equivalent to
     <pre class="example">          dlmread (<var>filename</var>, "," , ...)
</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_002ddlmread.html#doc_002ddlmread">dlmread</a>, <a href="doc_002ddlmwrite.html#doc_002ddlmwrite">dlmwrite</a>, <a href="doc_002dcsvwrite.html#doc_002dcsvwrite">csvwrite</a>. 
</p></blockquote></div>

<ul class="menu">
<li><a accesskey="1" href="Saving-Data-on-Unexpected-Exits.html#Saving-Data-on-Unexpected-Exits">Saving Data on Unexpected Exits</a>
</ul>

   </body></html>