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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Simple File I/O</title>
<meta name="description" content="GNU Octave: Simple File I/O">
<meta name="keywords" content="GNU Octave: Simple File I/O">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Basic-Input-and-Output.html#Basic-Input-and-Output" rel="up" title="Basic Input and Output">
<link href="Saving-Data-on-Unexpected-Exits.html#Saving-Data-on-Unexpected-Exits" rel="next" title="Saving Data on Unexpected Exits">
<link href="Terminal-Input.html#Terminal-Input" rel="prev" title="Terminal Input">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {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}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Simple-File-I_002fO"></a>
<div class="header">
<p>
Previous: <a href="Terminal-Input.html#Terminal-Input" accesskey="p" rel="prev">Terminal Input</a>, Up: <a href="Basic-Input-and-Output.html#Basic-Input-and-Output" accesskey="u" rel="up">Basic Input and Output</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Simple-File-I_002fO-1"></a>
<h4 class="subsection">14.1.3 Simple File I/O</h4>
<a name="index-saving-data"></a>
<a name="index-loading-data"></a>
<p>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>save_default_options</code> and <code>save_precision</code>.
</p>
<p>As an example the following code creates a 3-by-3 matrix and saves it
to the file ‘<samp>myfile.mat</samp>’.
</p>
<div class="example">
<pre class="example">A = [ 1:3; 4:6; 7:9 ];
save myfile.mat A
</pre></div>
<p>Once one or more variables have been saved to a file, they can be
read into memory using the <code>load</code> command.
</p>
<div class="example">
<pre class="example">load myfile.mat
A
-| A =
-|
-| 1 2 3
-| 4 5 6
-| 7 8 9
</pre></div>
<a name="XREFsave"></a><dl>
<dt><a name="index-save"></a>Command: <em></em> <strong>save</strong> <em>file</em></dt>
<dt><a name="index-save-1"></a>Command: <em></em> <strong>save</strong> <em>options file</em></dt>
<dt><a name="index-save-2"></a>Command: <em></em> <strong>save</strong> <em>options file <var>v1</var> <var>v2</var> …</em></dt>
<dt><a name="index-save-3"></a>Command: <em></em> <strong>save</strong> <em>options file -struct <var>STRUCT</var> <var>f1</var> <var>f2</var> …</em></dt>
<dd><p>Save the named variables <var>v1</var>, <var>v2</var>, …, in the file
<var>file</var>. The special filename ‘<samp>-</samp>’ 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 <samp>-struct</samp> modifier is used, fields <var>f1</var> <var>f2</var> …
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>save_default_options</code>.
</p>
<p>If save is invoked using the functional form
</p>
<div class="example">
<pre class="example">save ("-option1", …, "file", "v1", …)
</pre></div>
<p>then the <var>options</var>, <var>file</var>, and variable name arguments
(<var>v1</var>, …) must be specified as character strings.
</p>
<dl compact="compact">
<dt><code>-append</code></dt>
<dd><p>Append to the destination instead of overwriting.
</p>
</dd>
<dt><code>-ascii</code></dt>
<dd><p>Save a single matrix in a text file without header or any other information.
</p>
</dd>
<dt><code>-binary</code></dt>
<dd><p>Save the data in Octave’s binary data format.
</p>
</dd>
<dt><code>-float-binary</code></dt>
<dd><p>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.
</p>
</dd>
<dt><code>-hdf5</code></dt>
<dd><p>Save the data in <small>HDF5</small> format.
(HDF5 is a free, portable binary format developed by the National
Center for Supercomputing Applications at the University of Illinois.)
This format is only available if Octave was built with a link to the
<small>HDF5</small> libraries.
</p>
</dd>
<dt><code>-float-hdf5</code></dt>
<dd><p>Save the data in <small>HDF5</small> 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.
</p>
</dd>
<dt><code>-V7</code></dt>
<dt><code>-v7</code></dt>
<dt><code>-7</code></dt>
<dt><code>-mat7-binary</code></dt>
<dd><p>Save the data in <small>MATLAB</small>’s v7 binary data format.
</p>
</dd>
<dt><code>-V6</code></dt>
<dt><code>-v6</code></dt>
<dt><code>-6</code></dt>
<dt><code>-mat</code></dt>
<dt><code>-mat-binary</code></dt>
<dd><p>Save the data in <small>MATLAB</small>’s v6 binary data format.
</p>
</dd>
<dt><code>-V4</code></dt>
<dt><code>-v4</code></dt>
<dt><code>-4</code></dt>
<dt><code>-mat4-binary</code></dt>
<dd><p>Save the data in the binary format written by <small>MATLAB</small> version 4.
</p>
</dd>
<dt><code>-text</code></dt>
<dd><p>Save the data in Octave’s text data format. (default).
</p>
</dd>
<dt><code>-zip</code></dt>
<dt><code>-z</code></dt>
<dd><p>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.
This option is only available if Octave was built with a link to the zlib
libraries.
</p></dd>
</dl>
<p>The list of variables to save may use wildcard patterns containing
the following special characters:
</p>
<dl compact="compact">
<dt><code>?</code></dt>
<dd><p>Match any single character.
</p>
</dd>
<dt><code>*</code></dt>
<dd><p>Match zero or more characters.
</p>
</dd>
<dt><code>[ <var>list</var> ]</code></dt>
<dd><p>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 uppercase alphabetic characters.
</p>
<p>Wildcards may also be used in the field name specifications when using
the <samp>-struct</samp> modifier (but not in the struct name itself).
</p>
</dd>
</dl>
<p>Except when using the <small>MATLAB</small> binary data file format or the
‘<samp>-ascii</samp>’ format, saving global
variables also saves the global status of the variable. If the variable
is restored at a later time using ‘<samp>load</samp>’, it will be restored as a
global variable.
</p>
<p>The command
</p>
<div class="example">
<pre class="example">save -binary data a b*
</pre></div>
<p>saves the variable ‘<samp>a</samp>’ and all variables beginning with ‘<samp>b</samp>’ to
the file <samp>data</samp> in Octave’s binary format.
</p>
<p><strong>See also:</strong> <a href="#XREFload">load</a>, <a href="#XREFsave_005fdefault_005foptions">save_default_options</a>, <a href="#XREFsave_005fheader_005fformat_005fstring">save_header_format_string</a>, <a href="#XREFdlmread">dlmread</a>, <a href="#XREFcsvread">csvread</a>, <a href="Binary-I_002fO.html#XREFfread">fread</a>.
</p></dd></dl>
<p>There are three functions that modify the behavior of <code>save</code>.
</p>
<a name="XREFsave_005fdefault_005foptions"></a><dl>
<dt><a name="index-save_005fdefault_005foptions"></a>Built-in Function: <em><var>val</var> =</em> <strong>save_default_options</strong> <em>()</em></dt>
<dt><a name="index-save_005fdefault_005foptions-1"></a>Built-in Function: <em><var>old_val</var> =</em> <strong>save_default_options</strong> <em>(<var>new_val</var>)</em></dt>
<dt><a name="index-save_005fdefault_005foptions-2"></a>Built-in Function: <em></em> <strong>save_default_options</strong> <em>(<var>new_val</var>, "local")</em></dt>
<dd><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 <samp>-text</samp>.
</p>
<p>When called from inside a function with the <code>"local"</code> option, the
variable is changed locally for the function and any subroutines it calls.
The original variable value is restored when exiting the function.
</p>
<p><strong>See also:</strong> <a href="#XREFsave">save</a>.
</p></dd></dl>
<a name="XREFsave_005fprecision"></a><dl>
<dt><a name="index-save_005fprecision"></a>Built-in Function: <em><var>val</var> =</em> <strong>save_precision</strong> <em>()</em></dt>
<dt><a name="index-save_005fprecision-1"></a>Built-in Function: <em><var>old_val</var> =</em> <strong>save_precision</strong> <em>(<var>new_val</var>)</em></dt>
<dt><a name="index-save_005fprecision-2"></a>Built-in Function: <em></em> <strong>save_precision</strong> <em>(<var>new_val</var>, "local")</em></dt>
<dd><p>Query or set the internal variable that specifies the number of
digits to keep when saving data in text format.
</p>
<p>When called from inside a function with the <code>"local"</code> option, the
variable is changed locally for the function and any subroutines it calls.
The original variable value is restored when exiting the function.
</p></dd></dl>
<a name="XREFsave_005fheader_005fformat_005fstring"></a><dl>
<dt><a name="index-save_005fheader_005fformat_005fstring"></a>Built-in Function: <em><var>val</var> =</em> <strong>save_header_format_string</strong> <em>()</em></dt>
<dt><a name="index-save_005fheader_005fformat_005fstring-1"></a>Built-in Function: <em><var>old_val</var> =</em> <strong>save_header_format_string</strong> <em>(<var>new_val</var>)</em></dt>
<dt><a name="index-save_005fheader_005fformat_005fstring-2"></a>Built-in Function: <em></em> <strong>save_header_format_string</strong> <em>(<var>new_val</var>, "local")</em></dt>
<dd><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
‘<samp>#</samp>’ 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
</p>
<div class="smallexample">
<pre class="smallexample">"# Created by Octave VERSION, %a %b %d %H:%M:%S %Y %Z <USER@HOST>"
</pre></div>
<p>When called from inside a function with the <code>"local"</code> option, the
variable is changed locally for the function and any subroutines it calls.
The original variable value is restored when exiting the function.
</p>
<p><strong>See also:</strong> <a href="Timing-Utilities.html#XREFstrftime">strftime</a>, <a href="#XREFsave">save</a>.
</p></dd></dl>
<a name="XREFload"></a><dl>
<dt><a name="index-load"></a>Command: <em></em> <strong>load</strong> <em>file</em></dt>
<dt><a name="index-load-1"></a>Command: <em></em> <strong>load</strong> <em>options file</em></dt>
<dt><a name="index-load-2"></a>Command: <em></em> <strong>load</strong> <em>options file v1 v2 …</em></dt>
<dt><a name="index-load-3"></a>Command: <em>S =</em> <strong>load</strong> <em>("options", "file", "v1", "v2", …)</em></dt>
<dt><a name="index-load-4"></a>Command: <em></em> <strong>load</strong> <em>file options</em></dt>
<dt><a name="index-load-5"></a>Command: <em></em> <strong>load</strong> <em>file options v1 v2 …</em></dt>
<dt><a name="index-load-6"></a>Command: <em>S =</em> <strong>load</strong> <em>("file", "options", "v1", "v2", …)</em></dt>
<dd><p>Load the named variables <var>v1</var>, <var>v2</var>, …, 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>
<p>If load is invoked using the functional form
</p>
<div class="example">
<pre class="example">load ("-option1", …, "file", "v1", …)
</pre></div>
<p>then the <var>options</var>, <var>file</var>, and variable name arguments
(<var>v1</var>, …) must be specified as character strings.
</p>
<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>
<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>
<p>The <code>load</code> command can read data stored in Octave’s text and
binary formats, and <small>MATLAB</small>’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>
<p>Valid options for <code>load</code> are listed in the following table.
</p>
<dl compact="compact">
<dt><code>-force</code></dt>
<dd><p>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.
</p>
</dd>
<dt><code>-ascii</code></dt>
<dd><p>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.
</p>
</dd>
<dt><code>-binary</code></dt>
<dd><p>Force Octave to assume the file is in Octave’s binary format.
</p>
</dd>
<dt><code>-hdf5</code></dt>
<dd><p>Force Octave to assume the file is in <small>HDF5</small> format.
(<small>HDF5</small> is a free, portable binary format developed by the National
Center for Supercomputing Applications at the University of Illinois.)
Note that Octave can read <small>HDF5</small> files not created by itself, but may
skip some datasets in formats that it cannot support. This format is
only available if Octave was built with a link to the <small>HDF5</small> libraries.
</p>
</dd>
<dt><code>-import</code></dt>
<dd><p>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.
</p>
</dd>
<dt><code>-mat</code></dt>
<dt><code>-mat-binary</code></dt>
<dt><code>-6</code></dt>
<dt><code>-v6</code></dt>
<dt><code>-7</code></dt>
<dt><code>-v7</code></dt>
<dd><p>Force Octave to assume the file is in <small>MATLAB</small>’s version 6 or 7 binary
format.
</p>
</dd>
<dt><code>-mat4-binary</code></dt>
<dt><code>-4</code></dt>
<dt><code>-v4</code></dt>
<dt><code>-V4</code></dt>
<dd><p>Force Octave to assume the file is in the binary format written by
<small>MATLAB</small> version 4.
</p>
</dd>
<dt><code>-text</code></dt>
<dd><p>Force Octave to assume the file is in Octave’s text format.
</p></dd>
</dl>
<p><strong>See also:</strong> <a href="#XREFsave">save</a>, <a href="#XREFdlmwrite">dlmwrite</a>, <a href="#XREFcsvwrite">csvwrite</a>, <a href="Binary-I_002fO.html#XREFfwrite">fwrite</a>.
</p></dd></dl>
<a name="XREFfileread"></a><dl>
<dt><a name="index-fileread"></a>Function File: <em><var>str</var> =</em> <strong>fileread</strong> <em>(<var>filename</var>)</em></dt>
<dd><p>Read the contents of <var>filename</var> and return it as a string.
</p>
<p><strong>See also:</strong> <a href="Binary-I_002fO.html#XREFfread">fread</a>, <a href="#XREFtextread">textread</a>, <a href="Formatted-Input.html#XREFsscanf">sscanf</a>.
</p></dd></dl>
<a name="XREFnative_005ffloat_005fformat"></a><dl>
<dt><a name="index-native_005ffloat_005fformat"></a>Built-in Function: <em></em> <strong>native_float_format</strong> <em>()</em></dt>
<dd><p>Return the native floating point format as a string
</p></dd></dl>
<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 ‘<samp>myfile.txt</samp>’.
</p>
<div class="example">
<pre class="example">fid = fopen ("myfile.txt", "w");
fdisp (fid, "3/8 is ");
fdisp (fid, 3/8);
fclose (fid);
</pre></div>
<p>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>.
</p>
<a name="XREFfdisp"></a><dl>
<dt><a name="index-fdisp"></a>Built-in Function: <em></em> <strong>fdisp</strong> <em>(<var>fid</var>, <var>x</var>)</em></dt>
<dd><p>Display the value of <var>x</var> on the stream <var>fid</var>. For example:
</p>
<div class="example">
<pre class="example">fdisp (stdout, "The value of pi is:"), fdisp (stdout, pi)
-| the value of pi is:
-| 3.1416
</pre></div>
<p>Note that the output from <code>fdisp</code> always ends with a newline.
</p>
<p><strong>See also:</strong> <a href="Terminal-Output.html#XREFdisp">disp</a>.
</p></dd></dl>
<p>Octave can also read and write matrices text files such as comma
separated lists.
</p>
<a name="XREFdlmwrite"></a><dl>
<dt><a name="index-dlmwrite"></a>Function File: <em></em> <strong>dlmwrite</strong> <em>(<var>file</var>, <var>M</var>)</em></dt>
<dt><a name="index-dlmwrite-1"></a>Function File: <em></em> <strong>dlmwrite</strong> <em>(<var>file</var>, <var>M</var>, <var>delim</var>, <var>r</var>, <var>c</var>)</em></dt>
<dt><a name="index-dlmwrite-2"></a>Function File: <em></em> <strong>dlmwrite</strong> <em>(<var>file</var>, <var>M</var>, <var>key</var>, <var>val</var> …)</em></dt>
<dt><a name="index-dlmwrite-3"></a>Function File: <em></em> <strong>dlmwrite</strong> <em>(<var>file</var>, <var>M</var>, "-append", …)</em></dt>
<dt><a name="index-dlmwrite-4"></a>Function File: <em></em> <strong>dlmwrite</strong> <em>(<var>fid</var>, …)</em></dt>
<dd><p>Write the matrix <var>M</var> to the named file using delimiters.
</p>
<p><var>file</var> should be a file name or writable file ID given by <code>fopen</code>.
</p>
<p>The parameter <var>delim</var> specifies the delimiter to use to separate
values on a row.
</p>
<p>The value of <var>r</var> specifies the number of delimiter-only lines to
add to the start of the file.
</p>
<p>The value of <var>c</var> specifies the number of delimiters to prepend to
each line of data.
</p>
<p>If the argument <code>"-append"</code> is given, append to the end of
<var>file</var>.
</p>
<p>In addition, the following keyword value pairs may appear at the end
of the argument list:
</p>
<dl compact="compact">
<dt><code>"append"</code></dt>
<dd><p>Either <code>"on"</code> or <code>"off"</code>. See <code>"-append"</code> above.
</p>
</dd>
<dt><code>"delimiter"</code></dt>
<dd><p>See <var>delim</var> above.
</p>
</dd>
<dt><code>"newline"</code></dt>
<dd><p>The character(s) to use to separate each row. Three special cases
exist for this option. <code>"unix"</code> is changed into <code>"\n"</code>,
<code>"pc"</code> is changed into <code>"\r\n"</code>, and <code>"mac"</code> is changed
into <code>"\r"</code>. Other values for this option are kept as is.
</p>
</dd>
<dt><code>"roffset"</code></dt>
<dd><p>See <var>r</var> above.
</p>
</dd>
<dt><code>"coffset"</code></dt>
<dd><p>See <var>c</var> above.
</p>
</dd>
<dt><code>"precision"</code></dt>
<dd><p>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.
</p></dd>
</dl>
<div class="example">
<pre class="example">dlmwrite ("file.csv", reshape (1:16, 4, 4));
</pre></div>
<div class="example">
<pre class="example">dlmwrite ("file.tex", a, "delimiter", "&", "newline", "\\n")
</pre></div>
<p><strong>See also:</strong> <a href="#XREFdlmread">dlmread</a>, <a href="#XREFcsvread">csvread</a>, <a href="#XREFcsvwrite">csvwrite</a>.
</p></dd></dl>
<a name="XREFdlmread"></a><dl>
<dt><a name="index-dlmread"></a>Built-in Function: <em><var>data</var> =</em> <strong>dlmread</strong> <em>(<var>file</var>)</em></dt>
<dt><a name="index-dlmread-1"></a>Built-in Function: <em><var>data</var> =</em> <strong>dlmread</strong> <em>(<var>file</var>, <var>sep</var>)</em></dt>
<dt><a name="index-dlmread-2"></a>Built-in Function: <em><var>data</var> =</em> <strong>dlmread</strong> <em>(<var>file</var>, <var>sep</var>, <var>r0</var>, <var>c0</var>)</em></dt>
<dt><a name="index-dlmread-3"></a>Built-in Function: <em><var>data</var> =</em> <strong>dlmread</strong> <em>(<var>file</var>, <var>sep</var>, <var>range</var>)</em></dt>
<dt><a name="index-dlmread-4"></a>Built-in Function: <em><var>data</var> =</em> <strong>dlmread</strong> <em>(…, "emptyvalue", <var>EMPTYVAL</var>)</em></dt>
<dd><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>
<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>
<p>The <var>range</var> parameter may be a 4-element vector containing the upper
left and lower right corner <code>[<var>R0</var>,<var>C0</var>,<var>R1</var>,<var>C1</var>]</code>
where the lowest index value is zero. Alternatively, a spreadsheet style
range such as <code>"A2..Q15"</code> or <code>"T1:AA5"</code> can be used. The
lowest alphabetical index <code>'A'</code> refers to the first column. The
lowest row index is 1.
</p>
<p><var>file</var> should be a file name or file id given by <code>fopen</code>. In the
latter case, the file is read until end of file is reached.
</p>
<p>The <code>"emptyvalue"</code> option may be used to specify the value used to
fill empty fields. The default is zero.
</p>
<p><strong>See also:</strong> <a href="#XREFcsvread">csvread</a>, <a href="#XREFtextscan">textscan</a>, <a href="#XREFtextread">textread</a>, <a href="#XREFdlmwrite">dlmwrite</a>.
</p></dd></dl>
<a name="XREFcsvwrite"></a><dl>
<dt><a name="index-csvwrite"></a>Function File: <em></em> <strong>csvwrite</strong> <em>(<var>filename</var>, <var>x</var>)</em></dt>
<dt><a name="index-csvwrite-1"></a>Function File: <em></em> <strong>csvwrite</strong> <em>(<var>filename</var>, <var>x</var>, <var>dlm_opts</var>)</em></dt>
<dd><p>Write the matrix <var>x</var> to the file <var>filename</var> in
<span class="nolinebreak">comma-separated-value</span><!-- /@w --> format.
</p>
<p>This function is equivalent to
</p>
<div class="example">
<pre class="example">dlmwrite (<var>filename</var>, <var>x</var>, ",", …)
</pre></div>
<p><strong>See also:</strong> <a href="#XREFcsvread">csvread</a>, <a href="#XREFdlmwrite">dlmwrite</a>, <a href="#XREFdlmread">dlmread</a>.
</p></dd></dl>
<a name="XREFcsvread"></a><dl>
<dt><a name="index-csvread"></a>Function File: <em><var>x</var> =</em> <strong>csvread</strong> <em>(<var>filename</var>)</em></dt>
<dt><a name="index-csvread-1"></a>Function File: <em><var>x</var> =</em> <strong>csvread</strong> <em>(<var>filename</var>, <var>dlm_opts</var>)</em></dt>
<dd><p>Read the comma-separated-value file <var>filename</var> into the matrix <var>x</var>.
</p>
<p>This function is equivalent to
</p>
<div class="example">
<pre class="example"><var>x</var> = dlmread (<var>filename</var>, "," , …)
</pre></div>
<p><strong>See also:</strong> <a href="#XREFcsvwrite">csvwrite</a>, <a href="#XREFdlmread">dlmread</a>, <a href="#XREFdlmwrite">dlmwrite</a>.
</p></dd></dl>
<p>Formatted data from can be read from, or written to, text files as well.
</p>
<a name="XREFtextread"></a><dl>
<dt><a name="index-textread"></a>Function File: <em>[<var>a</var>, …] =</em> <strong>textread</strong> <em>(<var>filename</var>)</em></dt>
<dt><a name="index-textread-1"></a>Function File: <em>[<var>a</var>, …] =</em> <strong>textread</strong> <em>(<var>filename</var>, <var>format</var>)</em></dt>
<dt><a name="index-textread-2"></a>Function File: <em>[<var>a</var>, …] =</em> <strong>textread</strong> <em>(<var>filename</var>, <var>format</var>, <var>n</var>)</em></dt>
<dt><a name="index-textread-3"></a>Function File: <em>[<var>a</var>, …] =</em> <strong>textread</strong> <em>(<var>filename</var>, <var>format</var>, <var>prop1</var>, <var>value1</var>, …)</em></dt>
<dt><a name="index-textread-4"></a>Function File: <em>[<var>a</var>, …] =</em> <strong>textread</strong> <em>(<var>filename</var>, <var>format</var>, <var>n</var>, <var>prop1</var>, <var>value1</var>, …)</em></dt>
<dd><p>Read data from a text file.
</p>
<p>The file <var>filename</var> is read and parsed according to <var>format</var>. The
function behaves like <code>strread</code> except it works by parsing a file
instead of a string. See the documentation of <code>strread</code> for details.
</p>
<p>In addition to the options supported by <code>strread</code>, this function
supports two more:
</p>
<ul>
<li> <code>"headerlines"</code>:
The first <var>value</var> number of lines of <var>filename</var> are skipped.
</li><li> <code>"endofline"</code>:
Specify a single character or <code>"\r\n"</code>. If no value is given, it
will be inferred from the file. If set to "" (empty string) EOLs are
ignored as delimiters.
</li></ul>
<p>The optional input <var>n</var> specifies the number of data lines to read; in
this sense it differs slightly from the format repeat count in strread.
</p>
<p>If the format string is empty (not: omitted) and the file contains only
numeric data (excluding headerlines), textread will return a rectangular
matrix with the number of columns matching the number of numeric fields on
the first data line of the file. Empty fields are returned as zero values.
</p>
<p><strong>See also:</strong> <a href="Manipulating-Strings.html#XREFstrread">strread</a>, <a href="#XREFload">load</a>, <a href="#XREFdlmread">dlmread</a>, <a href="Formatted-Input.html#XREFfscanf">fscanf</a>, <a href="#XREFtextscan">textscan</a>.
</p></dd></dl>
<a name="XREFtextscan"></a><dl>
<dt><a name="index-textscan"></a>Function File: <em><var>C</var> =</em> <strong>textscan</strong> <em>(<var>fid</var>, <var>format</var>)</em></dt>
<dt><a name="index-textscan-1"></a>Function File: <em><var>C</var> =</em> <strong>textscan</strong> <em>(<var>fid</var>, <var>format</var>, <var>n</var>)</em></dt>
<dt><a name="index-textscan-2"></a>Function File: <em><var>C</var> =</em> <strong>textscan</strong> <em>(<var>fid</var>, <var>format</var>, <var>param</var>, <var>value</var>, …)</em></dt>
<dt><a name="index-textscan-3"></a>Function File: <em><var>C</var> =</em> <strong>textscan</strong> <em>(<var>fid</var>, <var>format</var>, <var>n</var>, <var>param</var>, <var>value</var>, …)</em></dt>
<dt><a name="index-textscan-4"></a>Function File: <em><var>C</var> =</em> <strong>textscan</strong> <em>(<var>str</var>, …)</em></dt>
<dt><a name="index-textscan-5"></a>Function File: <em>[<var>C</var>, <var>position</var>] =</em> <strong>textscan</strong> <em>(<var>fid</var>, …)</em></dt>
<dd><p>Read data from a text file or string.
</p>
<p>The string <var>str</var> or file associated with <var>fid</var> is read from and
parsed according to <var>format</var>. The function behaves like <code>strread</code>
except it can also read from file instead of a string. See the documentation
of <code>strread</code> for details.
</p>
<p>In addition to the options supported by <code>strread</code>, this function
supports a few more:
</p>
<ul>
<li> <code>"collectoutput"</code>:
A value of 1 or true instructs textscan to concatenate consecutive columns
of the same class in the output cell array. A value of 0 or false (default)
leaves output in distinct columns.
</li><li> <code>"endofline"</code>:
Specify <code>"\r"</code>, <code>"\n"</code> or <code>"\r\n"</code> (for CR, LF, or
CRLF). If no value is given, it will be inferred from the file. If set
to "" (empty string) EOLs are ignored as delimiters and added to
whitespace.
</li><li> <code>"headerlines"</code>:
The first <var>value</var> number of lines of <var>fid</var> are skipped.
</li><li> <code>"returnonerror"</code>:
If set to numerical 1 or true (default), return normally when read errors
have been encountered. If set to 0 or false, return an error and no data.
As the string or file is read by columns rather than by rows, and because
textscan is fairly forgiving as regards read errors, setting this option
may have little or no actual effect.
</li></ul>
<p>When reading from a character string, optional input argument <var>n</var>
specifies the number of times <var>format</var> should be used (i.e., to limit
the amount of data read).
When reading from file, <var>n</var> specifies the number of data lines to read;
in this sense it differs slightly from the format repeat count in strread.
</p>
<p>The output <var>C</var> is a cell array whose second dimension is determined
by the number of format specifiers.
</p>
<p>The second output, <var>position</var>, provides the position, in characters,
from the beginning of the file.
</p>
<p>If the format string is empty (not: omitted) and the file contains only
numeric data (excluding headerlines), textscan will return data in a number
of columns matching the number of numeric fields on the first data line of
the file.
</p>
<p><strong>See also:</strong> <a href="#XREFdlmread">dlmread</a>, <a href="Formatted-Input.html#XREFfscanf">fscanf</a>, <a href="#XREFload">load</a>, <a href="Manipulating-Strings.html#XREFstrread">strread</a>, <a href="#XREFtextread">textread</a>.
</p></dd></dl>
<p>The <code>importdata</code> function has the ability to work with a wide
variety of data.
</p>
<a name="XREFimportdata"></a><dl>
<dt><a name="index-importdata"></a>Function File: <em><var>A</var> =</em> <strong>importdata</strong> <em>(<var>fname</var>)</em></dt>
<dt><a name="index-importdata-1"></a>Function File: <em><var>A</var> =</em> <strong>importdata</strong> <em>(<var>fname</var>, <var>delimiter</var>)</em></dt>
<dt><a name="index-importdata-2"></a>Function File: <em><var>A</var> =</em> <strong>importdata</strong> <em>(<var>fname</var>, <var>delimiter</var>, <var>header_rows</var>)</em></dt>
<dt><a name="index-importdata-3"></a>Function File: <em>[<var>A</var>, <var>delimiter</var>] =</em> <strong>importdata</strong> <em>(…)</em></dt>
<dt><a name="index-importdata-4"></a>Function File: <em>[<var>A</var>, <var>delimiter</var>, <var>header_rows</var>] =</em> <strong>importdata</strong> <em>(…)</em></dt>
<dd><p>Import data from the file <var>fname</var>.
</p>
<p>Input parameters:
</p>
<ul>
<li> <var>fname</var>
The name of the file containing data.
</li><li> <var>delimiter</var>
The character separating columns of data. Use <code>\t</code> for tab.
(Only valid for ASCII files)
</li><li> <var>header_rows</var>
The number of header rows before the data begins. (Only valid for ASCII
files)
</li></ul>
<p>Different file types are supported:
</p>
<ul>
<li> ASCII table
<p>Import ASCII table using the specified number of header rows and
the specified delimiter.
</p>
</li><li> Image file
</li><li> <small>MATLAB</small> file
</li><li> Spreadsheet files (depending on external software)
</li><li> WAV file
</li></ul>
<p><strong>See also:</strong> <a href="#XREFtextscan">textscan</a>, <a href="#XREFdlmread">dlmread</a>, <a href="#XREFcsvread">csvread</a>, <a href="#XREFload">load</a>.
</p></dd></dl>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="Saving-Data-on-Unexpected-Exits.html#Saving-Data-on-Unexpected-Exits" accesskey="1">Saving Data on Unexpected Exits</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<div class="header">
<p>
Previous: <a href="Terminal-Input.html#Terminal-Input" accesskey="p" rel="prev">Terminal Input</a>, Up: <a href="Basic-Input-and-Output.html#Basic-Input-and-Output" accesskey="u" rel="up">Basic Input and Output</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|