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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>LOAD Load Variables From A File
</TITLE>
</HEAD>
<BODY>
<H2>LOAD Load Variables From A File
</H2>
<P>
Section: <A HREF=sec_io.html> Input/Ouput Functions </A>
<H3>Usage</H3>
Loads a set of variables from a file in a machine independent format.
The <code>load</code> function takes one argument:
<PRE>
load filename,
</PRE>
<P>
or alternately,
<PRE>
load('filename')
</PRE>
<P>
This command is the companion to <code>save</code>. It loads the contents of the
file generated by <code>save</code> back into the current context. Global and
persistent variables are also loaded and flagged appropriately. By
default, FreeMat assumes that files that end in a <code>.mat</code> or <code>.MAT</code>
extension are MATLAB-formatted files. Also, FreeMat assumes that
files that end in <code>.txt</code> or <code>.TXT</code> are ASCII files.
For other filenames, FreeMat first tries to open the file as a
FreeMat binary format file (as created by the <code>save</code> function).
If the file fails to open as a FreeMat binary file, then FreeMat
attempts to read it as an ASCII file.
You can force FreeMat to assume a particular format for the file
by using alternate forms of the <code>load</code> command. In particular,
<PRE>
load -ascii filename
</PRE>
<P>
will load the data in file <code>filename</code> as an ASCII file (space delimited
numeric text) loaded into a single variable in the current workspace
with the name <code>filename</code> (without the extension).
For MATLAB-formatted data files, you can use
<PRE>
load -mat filename
</PRE>
<P>
which forces FreeMat to assume that <code>filename</code> is a MAT-file, regardless
of the extension on the filename.
You can also specify which variables to load from a file (not from
an ASCII file - only single 2-D variables can be successfully saved and
retrieved from ASCII files) using the additional syntaxes of the <code>load</code>
command. In particular, you can specify a set of variables to load by name
<PRE>
load filename Var_1 Var_2 Var_3 ...
</PRE>
<P>
where <code>Var_n</code> is the name of a variable to load from the file.
Alternately, you can use the regular expression syntax
<PRE>
load filename -regexp expr_1 expr_2 expr_3 ...
</PRE>
<P>
where <code>expr_n</code> is a regular expression (roughly as expected by <code>regexp</code>).
Note that a simpler regular expression mechanism is used for this syntax
than the full mechanism used by the <code>regexp</code> command.
Finally, you can use <code>load</code> to create a variable containing the
contents of the file, instead of automatically inserting the variables
into the curent workspace. For this form of <code>load</code> you must use the
function syntax, and capture the output:
<PRE>
V = load('arg1','arg2',...)
</PRE>
<P>
which returns a structure <code>V</code> with one field for each variable
retrieved from the file. For ASCII files, <code>V</code> is a double precision
matrix.
<H3>Example</H3>
Here is a simple example of <code>save</code>/<code>load</code>. First, we save some variables to a file.
<PRE>
--> D = {1,5,'hello'};
--> s = 'test string';
--> x = randn(512,1);
--> z = zeros(512);
--> who
Variable Name Type Flags Size
D cell [1 3]
s char [1 11]
x double [512 1]
z double [512 512]
--> save loadsave.dat
</PRE>
<P>
Next, we clear the variables, and then load them back from the file.
<PRE>
--> clear D s x z
--> who
Variable Name Type Flags Size
ans double [0 0]
--> load loadsave.dat
--> who
Variable Name Type Flags Size
D cell [1 3]
ans double [0 0]
s char [1 11]
x double [512 1]
z double [512 512]
</PRE>
<P>
</BODY>
</HTML>
|