The save
and load
commands allow data to be written to and
read from disk files in various formats. The default format of files
written by the save
command can be controlled using the functions
default_save_options
and save_precision
.
As an example the following code creates a 3-by-3 matrix and saves it to the file ‘myfile.mat’.
A = [ 1:3; 4:6; 7:9 ]; save myfile.mat A
Once one or more variables have been saved to a file, they can be
read into memory using the load
command.
load myfile.mat A -| A = -| -| 1 2 3 -| 4 5 6 -| 7 8 9
Save the named variables v1, v2, ..., in the file file. The special filename ‘-’ 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 -struct modifier is used, fields f1 f2 ... of the scalar structure STRUCT are saved as if they were variables with corresponding names. Valid options for the
save
command are listed in the following table. Options that modify the output format override the format specified bydefault_save_options
.If save is invoked using the functional form
save ("-option1", ..., "file", "v1", ...)then the options, file, and variable name arguments (v1, ...) must be specified as character strings.
-append
- Append to the destination instead of overwriting.
-ascii
- Save a single matrix in a text file without header or any other information.
-binary
- Save the data in Octave's binary data format.
-float-binary
- 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.
-hdf5
- 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.) This format is only available if Octave was built with a link to the hdf5 libraries.
-float-hdf5
- 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.
-V7
-v7
-7
-mat7-binary
- Save the data in matlab's v7 binary data format.
-V6
-v6
-6
-mat
-mat-binary
- Save the data in matlab's v6 binary data format.
-V4
-v4
-4
-mat4-binary
- Save the data in the binary format written by matlab version 4.
-text
- Save the data in Octave's text data format. (default).
-zip
-z
- 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.
The list of variables to save may use wildcard patterns containing the following special characters:
?
- Match any single character.
*
- Match zero or more characters.
[
list]
- Match the list of characters specified by list. If the first character is
!
or^
, match all characters except those specified by list. For example, the pattern[a-zA-Z]
will match all lower and uppercase alphabetic characters.Wildcards may also be used in the field name specifications when using the -struct modifier (but not in the struct name itself).
Except when using the matlab binary data file format or the ‘-ascii’ format, saving global variables also saves the global status of the variable. If the variable is restored at a later time using ‘load’, it will be restored as a global variable.
The command
save -binary data a b*saves the variable ‘a’ and all variables beginning with ‘b’ to the file data in Octave's binary format.
See also: load, default_save_options, save_header_format_string, dlmread, csvread, fread.
Load the named variables v1, v2, ..., from the file file. If no variables are specified then all variables found in the file will be loaded. As with
save
, 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.If load is invoked using the functional form
load ("-option1", ..., "file", "v1", ...)then the options, file, and variable name arguments (v1, ...) must be specified as character strings.
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.
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,
load
returns a structure with members corresponding to the names of the variables in the file.The
load
command can read data stored in Octave's text and binary formats, and matlab'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).Valid options for
load
are listed in the following table.
-force
- 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.
-ascii
- 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.
-binary
- Force Octave to assume the file is in Octave's binary format.
-hdf5
- 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. This format is only available if Octave was built with a link to the hdf5 libraries.
-import
- 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.
-mat
-mat-binary
-6
-v6
-7
-v7
- Force Octave to assume the file is in matlab's version 6 or 7 binary format.
-mat4-binary
-4
-v4
-V4
- Force Octave to assume the file is in the binary format written by matlab version 4.
-text
- Force Octave to assume the file is in Octave's text format.
There are three functions that modify the behavior of save
.
Query or set the internal variable that specifies the default options for the
save
command, and defines the default format. Typical values include"-ascii"
,"-text -zip"
. The default value is -text.When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.
See also: save.
Query or set the internal variable that specifies the number of digits to keep when saving data in text format.
When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.
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
strftime
and should begin with the character ‘#’ and contain no newline characters. If the value ofsave_header_format_string
is the empty string, the header comment is omitted from text-format data files. The default value is"# Created by Octave VERSION, %a %b %d %H:%M:%S %Y %Z <USER@HOST>"When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.
It is possible to write data to a file in a similar way to the
disp
function for writing data to the screen. The fdisp
works just like disp
except its first argument is a file pointer
as created by fopen
. As an example, the following code writes
to data ‘myfile.txt’.
fid = fopen ("myfile.txt", "w"); fdisp (fid, "3/8 is "); fdisp (fid, 3/8); fclose (fid);
See Opening and Closing Files, for details on how to use fopen
and fclose
.
Display the value of x on the stream fid. For example:
fdisp (stdout, "The value of pi is:"), fdisp (stdout, pi) -| the value of pi is: -| 3.1416Note that the output from
fdisp
always ends with a newline.See also: disp.
Octave can also read and write matrices text files such as comma separated lists.
Write the matrix M to the named file using delimiters.
file should be a file name or writable file ID given by
fopen
.The parameter delim specifies the delimiter to use to separate values on a row.
The value of r specifies the number of delimiter-only lines to add to the start of the file.
The value of c specifies the number of delimiters to prepend to each line of data.
If the argument
"-append"
is given, append to the end of file.In addition, the following keyword value pairs may appear at the end of the argument list:
- "append"
- Either ‘"on"’ or ‘"off"’. See ‘"-append"’ above.
- "delimiter"
- See delim above.
- "newline"
- The character(s) to use to separate each row. Three special cases exist for this option. ‘"unix"’ is changed into "\n", ‘"pc"’ is changed into "\r\n", and ‘"mac"’ is changed into "\r". Other values for this option are kept as is.
- "roffset"
- See r above.
- "coffset"
- See c above.
- "precision"
- 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.
dlmwrite ("file.csv", reshape (1:16, 4, 4));dlmwrite ("file.tex", a, "delimiter", "&", "newline", "\\n")
Read the matrix data from a text file. If not defined the separator between fields is determined from the file itself. Otherwise the separation character is defined by sep.
Given two scalar arguments r0 and c0, 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.
The range parameter may be a 4-element vector containing the upper left and lower right corner
[
R0,
C0,
R1,
C1]
where the lowest index value is zero. Alternatively, a spreadsheet style range such as 'A2..Q15' or 'T1:AA5' can be used. The lowest alphabetical index 'A' refers to the first column. The lowest row index is 1.file should be a file name or file id given by
fopen
. In the latter case, the file is read until end of file is reached.The "emptyvalue" option may be used to specify the value used to fill empty fields. The default is zero.
Write the matrix x to the file filename in comma-separated-value format.
This function is equivalent to
dlmwrite (filename, x, ",", ...)
Read the comma-separated-value file filename into the matrix x.
This function is equivalent to
x = dlmread (filename, "," , ...)
Formatted data from can be read from, or written to, text files as well.
Read data from a text file.
The file filename is read and parsed according to format. The function behaves like
strread
except it works by parsing a file instead of a string. See the documentation ofstrread
for details.In addition to the options supported by
strread
, this function supports two more:
- "headerlines": The first value number of lines of filename are skipped.
- "endofline": Specify a single character or "\r\n". If no value is given, it will be inferred from the file. If set to "" (empty string) EOLs are ignored as delimiters.
The optional input n specifes the number of times to use format when parsing, i.e., the format repeat count.
Read data from a text file or string.
The file associated with fid is read and parsed according to format. The function behaves like
strread
except it works by parsing a file instead of a string. See the documentation ofstrread
for details.In addition to the options supported by
strread
, this function supports a few more:
- "collectoutput": 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.
- "endofline": Specify "\r", "\n" or "\r\n" (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.
- "headerlines": The first value number of lines of fid are skipped.
- "returnonerror": 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.
The optional input n specifes the number of times to use format when parsing, i.e., the format repeat count.
The output C is a cell array whose length is given by the number of format specifiers.
The second output, position, provides the position, in characters, from the beginning of the file.