1 <?xml version="1.0" encoding="UTF-8"?>
3 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4 * Copyright (C) XXXX-2008 - INRIA
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
6 * Copyright (C) 2018 - Samuel GOUGEON
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
16 <refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
17 xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns4="http://www.w3.org/1999/xhtml"
18 xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook"
19 xmlns:scilab="http://www.scilab.org" xml:id="save" xml:lang="en">
21 <refname>save</refname>
22 <refpurpose>Saves some chosen variables in a binary data file
29 save(filename, x1, x2,...,xn)
30 save(filename, x1, x2,...,xn, "-append")
34 <title>Arguments</title>
39 <para>Character string containing the path of the file.</para>
46 Names (as strings) of Scilab variables to be saved. Objects returned by
47 expressions -- like gcf() -- can't be directly saved. They must be
48 explicitly named: <literal>f = gcf(); save(filename, "f")</literal>.
55 <title>Description</title>
57 <literal>save(filename)</literal> saves in the file defined by <literal>filename</literal>
58 <emphasis>all</emphasis> Scilab variables defined by the user, and reachable at the
65 When some global variables are defined, only their local counterparts
66 -- if any -- can be saved. Hence, the "global status" is never saved.
71 If a variable is a graphic handle, <literal>save(..)</literal> records all
72 the corresponding <emphasis>descending</emphasis>
73 <link linkend="graphics_entities">graphical entity</link> definition,
74 including its existing children -- if any.
79 Saving a XML pointer is possible but does not save the pointed object. If in
80 the meantime the XML object is deleted, restoring its pointer won't restore
81 the object. Please see the dedicated example.
86 TCL variables defined from Scilab (with <link linkend="TCL_SetVar">TCL_SetVar</link>)
87 can't be saved. Only their counterparts in Scilab -- if any -- can be considered.
92 Scilab save/load functions use the Scilab Open Data (SOD) binary format,
93 based on the HDF-5 storage format. The SOD format is described
94 <link linkend="save_format">there</link>. Scilab binary data files can be read
95 and handled through external HDF-5 libraries or applications.
101 When only some variables chosen among all defined ones must be saved, their names must
102 be listed after the filename, as in <literal>save(filename,"x","y")</literal>. If one
103 indicated variable is not defined, the file is not created or overwritten and an error
107 The <literal>"-append"</literal> keyword can be used in option at any place after
108 the <varname>filename</varname>. Then, listed variables are appended to the existing
109 file. If some listed variables already exist in the file, their stored values are updated.
112 Saved variables can be reloaded by the
114 <link linkend="load">load</link>
119 The archives files generated with save() are portable to other operating systems and
120 architectures (little and big endian automatically handled).
123 Built-in functions can't be saved.
127 <title>Examples</title>
129 With homogeneous arrays of simple data types:
131 <programlisting role="example"><![CDATA[
132 // For each created object, we make a current copy for comparison after restoring.
133 b = [%t %f %f]; bc = b; // Booleans
134 i = int8([0 -17 38 ]); ic = i; // Encoded integers
135 d = [%pi %e %inf %i]; dc = d; // Decimal numbers (real or complex numbers)
136 p = (1-%z)^[1 2]; pc = p; // Polynomials (real or complex coefficients)
137 r = %z./(1-%z)^[1 2]; rc = r; // Rationals (real or complex coefficients)
138 t = ["Sci" "البرمجیات"]; tc = t; // Texts
139 sp = sprand(4,10,0.1); spc = sp; // Sparse matrix of numbers
140 spb = sp < 0.5; spbc = spb;// Sparse matrix of boolean numbers
142 path = TMPDIR + "/test.dat";
143 save(path, "b","i","d","p","r","t","sp","spb")
144 clear b i d p r t sp spb
145 isdef(["b" "i" "d" "p" "r" "t" "sp" "spb"],"l")
148 isdef(["b" "i" "d" "p" "r" "t" "sp" "spb"],"l")
149 [b==bc, i==ic, d==dc, t==tc, p==pc, r==rc, and(sp==spc), and(spb==spbc)]
152 --> path = TMPDIR + "/test.dat";
153 --> save(path, "b","i","d","p","r","t","sp","spb")
154 --> clear b i d p r t sp spb
155 --> isdef(["b" "i" "d" "p" "r" "t" "sp" "spb"],"l")
159 --> listvarinfile(path);
161 -------------------------------------------------------------
165 p polynomial 1 by 2 40
168 spb boolean sparse 4 by 10 160
172 --> isdef(["b" "i" "d" "p" "r" "t" "sp" "spb"],"l")
176 --> [b==bc, i==ic, d==dc, t==tc, p==pc, r==rc, and(sp==spc), and(spb==spbc)]
178 T T T T T T T T T T T T T T T T T
181 With a user-defined macro:
183 <programlisting role="example"><![CDATA[
184 path = TMPDIR + "/test.dat";
185 function myTest(), disp("User-defined macros can be saved"), endfunction
194 --> listvarinfile(path);
196 -------------------------------------------------------------
197 myTest macro 1 by 1 0
201 User-defined macros can be saved
204 With (nested) heterogeneous containers:
206 <programlisting role="example"><![CDATA[
208 L = list(%pi,%t,%s^2,"abc",{"Scilab" 2},list(%e,%z/(1-%z))); Lc = L;
210 c = {%pi, %t ; %s^2, "abc"}; cc = c;
211 // array of structures
212 s = struct("n",%i+1, "p",(1+%z)^3, "L",L, "c",c); sc = s;
214 path = TMPDIR + "/test.dat";
215 save(path, "L", "c", "s")
217 isdef(["L" "c" "s"], "l")
220 [isequal(L,Lc) isequal(c,cc) isequal(s,sc)]
223 --> isdef(["L" "c" "s"], "l")
227 --> listvarinfile(path);
229 -------------------------------------------------------------
235 --> [isequal(L,Lc) isequal(c,cc) isequal(s,sc)]
240 With some graphic handles:
242 <programlisting role="example"><![CDATA[
244 subplot(1,2,1), param3d()
245 subplot(1,2,2), plot2d4(), xtitle("plot2d4()"); a = gca();
246 path = TMPDIR + "/test.dat";
247 save(path, "a"); // We save only the right axes (plot2d4)
249 xload(path); // The right axes is restored and rendered
250 gcf().axes_size = [700 300];
253 subplot(1,2,1), param3d()
254 subplot(1,2,2), plot2d4(), xtitle("plot2d4()"); a = gca();
255 path = TMPDIR + "/test.dat";
256 save(path, "a"); // We save only the right axes (hist3d)
258 xload(path); // The right axes is restored and rendered
259 gcf().axes_size = [700 300];
262 With a global variable:
264 <programlisting role="example"><![CDATA[
265 path = TMPDIR + "/test.dat";
270 clear a, clearglobal a
272 isglobal("a") // The global attribute was not saved and so is not restored
279 --> clear a, clearglobal a
290 <programlisting role="example"><![CDATA[
291 path = TMPDIR + "/test.dat";
292 doc = xmlReadStr("<root><b>Hello</b></root>");
294 clear doc // This does not delete the document
295 load(path); doc // We restore the saved pointer to the document. It is still valid.
296 // Let's delete both the document and the pointer to it:
297 xmlDelete(doc), clear doc
298 load(path); // We restore the saved pointer (to the unexisting document)
299 isdef("doc","l") // The pointer is restored...
300 xmlIsValidObject doc // but not the pointed document
303 --> path = TMPDIR + "/test.dat";
304 --> doc = xmlReadStr("<root><b>Hello</b></root>");
305 --> save(path, "doc")
313 --> xmlDelete(doc), clear doc
319 --> xmlIsValidObject doc
324 With the "-append" option:
326 <programlisting role="example"><![CDATA[
328 path = TMPDIR + "/test.dat";
329 save(path, "a", "b");
333 save(path, "a", "c", "-append");
339 --> save(path, "a", "b");
340 --> listvarinfile(path);
342 -------------------------------------------------------------
348 --> save(path, "a", "c", "-append");
349 --> listvarinfile(path);
351 -------------------------------------------------------------
357 --> load(path, "a"); a // has been updated
362 save() can't save TCL variables:
364 <programlisting role="example"><![CDATA[
365 TCL_SetVar("myPi", 3.14); // Creates the myPi variable in the TCL session
366 myPi // => error: the variable is a TCL one, not a Scilab one => It can't be save()d
370 Undefined variable: myPi
373 <refsection role="see also">
374 <title>See also</title>
375 <simplelist type="inline">
377 <link linkend="load">load</link>
380 <link linkend="save_format">save_format</link>
383 <link linkend="savematfile">savematfile</link>
386 <link linkend="xsave">xsave</link>
389 <link linkend="saveGui">saveGui</link>
392 <link linkend="write">write</link>
395 <link linkend="TCL_SetVar">TCL_SetVar</link>
400 <title>History</title>
403 <revnumber>5.0.0</revnumber>
405 All <link linkend="uimenu">uimenu</link> or
406 <link linkend="uicontrol">uicontrol</link> handles are also saved by this function.
410 <revnumber>5.4.0</revnumber>
414 When called with variables names (character string) as input, variables
415 are saved in SOD format (HDF5-based).
418 The Scilab <5.4 binary data format is deprecated.
421 Using save() with a file descriptor as first input argument is deprecated.
427 <revnumber>6.0</revnumber>
431 save() no longer supports the old Scilab <5.4 data format.
434 The syntaxes save(fid) and save(fid, x1,..) with a file id are no longer