1 ## Copyright (C) 2003 Tomer Altman <taltman@lbl.gov>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
16 ## append_save M-file function
18 ## Objective: be able to add variables to existing save files. Works for
19 ## all the types of save files that "save" supports.
22 ## 1) A string which specifies the existing save file.
23 ## 2) The options you need to pass to the 'save' function to save to the
24 ## file type that you want.
25 ## 3) A 1x2 cell, with the first element being a string representation
26 ## of the variable/symbol that you're trying to add, followed by the
27 ## actual variable/symbol itself.
28 ## 4) Any number of additional 1x2 cells, following the same format as
29 ## the 3rd argument specified immediately before this one.
32 ## Currently, none. But there might be some debugging / error-code
33 ## messages in the future.
36 ## octave> B = ones(2,2);
37 ## octave> append_save( "test.txt", "-binary", {"B", B } )
40 function [ return_value ] = append_save ( filename,
48 error("append_save: needs three arguments.");
49 elseif ( !ischar(filename) )
50 error("append_save: filename must be a string.");
51 elseif ( !ischar(option) )
52 error("append_save: option must be a string." );
53 elseif ( !iscell(var_val_cell) )
54 error("append_save: variable-value pairs must be cells.")
57 current_cell = varargin(i);
58 if ( !iscell(current_cell) )
59 error("append_save: variable-value pairs must be cells.");
60 elseif ( ( columns( current_cell ) != 2 )
61 || ( rows( current_cell ) != 1 ) )
62 error("append_save: variable-value pairs must be 1x2 cells.")
63 elseif ( !ischar(current_cell{1} ) )
64 error("append_save: variable in pair must be a string." )
69 ## First step: load into the environment what is already stuffed in
70 ## the target file. Then, add their name to the list for "save".
74 eval([ "load -force ", \
80 num_orig_vars = rows(env1);
82 # Not really 'current' env...
83 num_current_vars = rows(env2);
85 num_new_vars = num_current_vars - num_orig_vars;
89 ## This double 'for loop' weeds out only the loaded vars for
94 for i=1:num_current_vars
96 current_var = env2{i,1};
100 for j=1:num_orig_vars
102 if ( strcmp( env1{j,1}, env2{i,1} )
104 strcmp( env2{i,1}, "env1" ) )
114 var_name = env2{i,1};
116 var_str = [ var_str, " ", var_name, " " ];
125 ## Second step: load into the environment the variable pairs. Then,
126 ## add the name to the string for "save".
128 var_name = var_val_cell{1};
130 var_val = var_val_cell{2};
134 eval([ var_name, "=temp;" ]);
136 var_str = [ var_str, " ", var_name, " " ];
138 ## Third step: do the same as step two, but loop through the possible
139 ## variable arguments.
143 current_cell = varargin(i);
145 var_name = current_cell{1};
147 var_val = current_cell{2};
151 eval([ var_name, "=temp;" ]);
153 var_str = [ var_str, " ", var_name, " " ];
159 ## Finally, save all of the variables into the target file:
163 filename, " ", var_str; ] );