[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: saving struct data
From: |
A Scott Hodel |
Subject: |
Re: saving struct data |
Date: |
Wed, 21 Apr 1999 07:22:21 -0500 (CDT) |
There was a patch to the octave sources posted awhile back that will
allow this. I wrote scripts to convert aribitrary variables to
a vector and back again for bleeding edge releases; includes lists.
If you're using octave-2.0.x, you'll need to remove references to the
list functions (is_list, nth, etc).
I've been using them for a couple of weeks sor so, so they're
"experimentally" bug free. Here they are:
# Copyright (C) 1999 A. Scottedward Hodel
#
# This file is part of Octave.
#
# Octave is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# Octave is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with Octave; see the file COPYING. If not, write to the Free
# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
function vv = var2vec(var)
# function vv = var2vec(var)
# encode an arbitrary octave variable into a vector appropriate for
# octave save/load comands
# inputs: var: octave variable
# outputs: vec: vector encoding of vector value
# encoding:
# 1st number: variable type:
# 0 numerical
# followed by [rows columns vec(var)' ]
# 1 string
# followed by [rows columns vec(1.0*var)' ]
# 2 structure
# followed by [number_members member_name member value ...]
# 3 list
# followed by [number_elements var2vec(nth(var,1)) ... ]
vartype = 3*is_list(var) + 2*is_struct(var) + 1*isstr(var);
switch(vartype)
case(0), # numerial
vv = [ vartype; rows(var); columns(var) ];
if(!isempty(var)) vv = [vv; vec(var)]; endif
case(1), # string
save_val = implicit_str_to_num_ok;
implicit_str_to_num_ok = 1;
vv = var2vec(1.0*var);
vv(1) = vartype;
implicit_str_to_num_ok = save_val;
case(2), # structure
elems = struct_elements(var);
nelems = rows(elems);
vv = [vartype; nelems];
for kk=1:nelems
varname = deblank(elems(kk,:));
vnvec = var2vec(varname);
eval(sprintf("valvec = var2vec(var.%s);",varname));
vv = [vv; vnvec; valvec];
endfor
case(3), # list
vv = [vartype; length(var)];
for kk=1:length(var)
vv = [vv; var2vec(nth(var,kk))];
endfor
otherwise,
error("vartype %d not implemented yet\n",vartype);
vv = vartype;
endswitch
endfunction
# Copyright (C) 1999 A. Scottedward Hodel
#
# This file is part of Octave.
#
# Octave is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# Octave is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with Octave; see the file COPYING. If not, write to the Free
# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
function [var, vvrem] = vec2var(vv)
# function var = vec2var(vv)
# recover a variable from its var2vec encoding (see var2vec for format)
# inputs: vec: vector in var2vec output form
# outputs: var: corresponding octave variable
# vvrem: remaining part of vector (unused portion)
vv = vec(vv);
vartype = vv(1);
switch(vartype)
case(0),
# numerical value
vrows = vv(2); vcols = vv(3); vend = 3+vrows*vcols ;
if(vrows*vcols > 0) var = reshape( vv(4:vend), vrows,vcols );
else var = zeros(vrows,vcols);
endif
vvrem = vv((vend+1):length(vv));
case(1),
# string value
vv(1) = 0;
[var,vvrem] = vec2var(vv);
var = setstr(var);
case(2),
# structure value
nelems = vv(2);
vv = vv(3:length(vv),1);
for kk=1:nelems
[varname,vv] = vec2var(vv);
[elemval,vv] = vec2var(vv);
eval(sprintf("var.%s = elemval;",varname));
endfor
vvrem = vv;
case(3),
# list
var = list();
nelems = vv(2);
vv = vv(3:length(vv),1);
for kk=1:nelems
[var(kk), vv] = vec2var(vv);
endfor
vvrem = vv;
otherwise,
error("vec2var: vartype %d not yet implemented\n",vartype);
endswitch
endfunction
- Re: saving struct data,
A Scott Hodel <=