]> Creatis software - CreaPhase.git/blob - octave_packages/io-1.0.19/append_save.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / io-1.0.19 / append_save.m
1 ## Copyright (C) 2003 Tomer Altman <taltman@lbl.gov>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## append_save M-file function
17 ##
18 ## Objective: be able to add variables to existing save files. Works for
19 ## all the types of save files that "save" supports.
20 ## 
21 ## Input: 
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.
30 ##
31 ## Output:
32 ## Currently, none. But there might be some debugging / error-code
33 ## messages in the future.
34 ##
35 ## Example:
36 ## octave> B = ones(2,2);
37 ## octave> append_save( "test.txt", "-binary", {"B", B } )
38
39
40 function [ return_value ] = append_save ( filename,
41                                           option,
42                                           var_val_cell,
43                                           varargin )
44
45   ## Input checking:
46
47   if ( nargin < 3 )
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.")
55   elseif ( nargin > 3 )
56     for i=1:(nargin-3)
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." )
65       endif
66     endfor
67   endif
68
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".
71
72   env1 = who;
73   
74   eval([ "load -force ", \
75         option, " ",    \
76         filename ] );
77
78   env2 = who;
79   
80   num_orig_vars = rows(env1);
81
82                                 # Not really 'current' env...
83   num_current_vars = rows(env2);
84
85   num_new_vars = num_current_vars - num_orig_vars;
86
87   var_str = "";
88
89   ## This double 'for loop' weeds out only the loaded vars for
90   ## inclusion.
91
92   if ( num_new_vars )
93
94     for i=1:num_current_vars
95
96       current_var = env2{i,1};
97
98       old_bool = 0;
99
100       for j=1:num_orig_vars
101
102         if ( strcmp( env1{j,1}, env2{i,1} ) 
103              ||
104              strcmp( env2{i,1}, "env1" ) )
105
106           old_bool = 1;
107
108         endif
109
110       endfor
111
112       if ( old_bool == 0 )
113
114         var_name = env2{i,1};
115
116         var_str = [ var_str, " ", var_name, " " ];
117
118       endif
119
120     endfor
121
122   endif
123
124   
125   ## Second step: load into the environment the variable pairs. Then,
126   ## add the name to the string for "save".
127
128   var_name = var_val_cell{1};
129
130   var_val = var_val_cell{2};
131
132   temp = var_val;
133
134   eval([ var_name, "=temp;" ]);
135
136   var_str = [ var_str, " ", var_name, " " ];
137
138   ## Third step: do the same as step two, but loop through the possible
139   ## variable arguments.
140
141   for i=1:(nargin-3)
142
143     current_cell = varargin(i);
144
145     var_name = current_cell{1};
146     
147     var_val = current_cell{2};
148     
149     temp = var_val;
150     
151     eval([ var_name, "=temp;" ]);
152     
153     var_str = [ var_str, " ", var_name, " " ];
154
155     var_str
156
157   endfor
158
159   ## Finally, save all of the variables into the target file:
160
161   eval( [ "save ", \
162            option, " ",  \
163            filename, " ", var_str; ] );
164
165 endfunction