Re: appending data in a binary file
[email protected] Thu, 6 Nov 2003 20:39:59 +0000 (UTC)
| Newsgroups | gmane.comp.gnu.octave.general,gmane.comp.gnu.octave.sources |
|---|---|
| Message-ID | <[email protected]> |
Fresh from the oven! :-)
Attached, please find my M-file named "apend_save.m" .
function: append_save
arguments: 3+additional
1st: string for file name
2nd: string for options to pass to load & save ( i.e., "-binary" )
3rd: a 1x2 cell with the first cell element being a string for the
variable name, and the 2nd cell being the variable value.
4th: can accept additional cells of the same format as "3rd" above.
Exampe:
octave> A = zeros(2,2);
octave> save -binary "test.txt" A
octave> clear A
octave> B = ones(2,2);
octave> append_save( "test.txt", "-binary", {"B", B } )
octave> clear B
octave> A
error: `A' undefined near line 147 column 1
octave> B
error: `B' undefined near line 147 column 1
octave> load -binary "test.txt"
octave> A
A =
0 0
0 0
octave> B
B =
1 1
1 1
-- <end example> --
Please give me feedback on this hack.
Thanks,
~Tomer
On Nov 6, 2003 at 10:27pm, charo wrote:
rbalbo >Hi all,
rbalbo >
rbalbo >is there a way to append data to an Octave binary file?
rbalbo >
rbalbo >Thanks,
rbalbo >
rbalbo >R
append_save.m
(text/plain, 2.7 KB)
## Tomer Altman taltman-at-lbl-dot-gov
function [ return_value ] = append_save ( filename,
option,
var_val_cell,
varargin )
## Input checking:
if ( nargin < 3 )
error("append_save: needs three arguments.");
elseif ( !isstr(filename) )
error("append_save: filename must be a string.");
elseif ( !isstr(option) )
error("append_save: option must be a string." );
elseif ( !iscell(var_val_cell) )
error("append_save: variable-value pairs must be cells.")
elseif ( nargin > 3 )
for i=1:(nargin-3)
current_cell = varargin(i);
if ( !iscell(current_cell) )
error("append_save: variable-value pairs must be cells.");
elseif ( ( columns( current_cell ) != 2 )
|| ( rows( current_cell ) != 1 ) )
error("append_save: variable-value pairs must be 1x2 cells.")
elseif ( !isstr(current_cell{1} ) )
error("append_save: variable in pair must be a string." )
endif
endfor
endif
## First step: load into the environment what is already stuffed in
## the target file. Then, add their name to the list for "save".
env1 = who;
eval([ "load -force ", \
option, " ", \
filename ] );
env2 = who;
num_orig_vars = rows(env1);
# Not really 'current' env...
num_current_vars = rows(env2);
num_new_vars = num_current_vars - num_orig_vars;
var_str = "";
## This double 'for loop' weeds out only the loaded vars for
## inclusion.
if ( num_new_vars )
for i=1:num_current_vars
current_var = env2{i,1};
old_bool = 0;
for j=1:num_orig_vars
if ( strcmp( env1{j,1}, env2{i,1} )
||
strcmp( env2{i,1}, "env1" ) )
old_bool = 1;
endif
endfor
if ( old_bool == 0 )
var_name = env2{i,1};
var_str = [ var_str, " ", var_name, " " ];
endif
endfor
endif
## Second step: load into the environment the variable pairs. Then,
## add the name to the string for "save".
var_name = var_val_cell{1};
var_val = var_val_cell{2};
temp = var_val;
eval([ var_name, "=temp;" ]);
var_str = [ var_str, " ", var_name, " " ];
## Third step: do the same as step two, but loop through the possible
## variable arguments.
for i=1:(nargin-3)
current_cell = varargin(i);
var_name = current_cell{1};
var_val = current_cell{2};
temp = var_val;
eval([ var_name, "=temp;" ]);
var_str = [ var_str, " ", var_name, " " ];
var_str
endfor
## Finally, save all of the variables into the target file:
eval( [ "save ", \
option, " ", \
filename, " ", var_str; ] );
endfunction