Mercurial > hg > octave-avbm
changeset 9849:87fd803c583b
rewrite getfield and setfield
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 20 Nov 2009 14:46:13 +0100 |
parents | 6867676107f3 |
children | 85f6c386f2bb |
files | scripts/ChangeLog scripts/miscellaneous/getfield.m scripts/miscellaneous/setfield.m |
diffstat | 3 files changed, 34 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,9 @@ +2009-11-20 Jaroslav Hajek <highegg@gmail.com> + + * script/miscellaneous/getfield: Deblank field names, gripe on invalid + index. + * script/miscellaneous/setfield: Rewrite using subsasgn. + 2009-11-22 Michael Goffioul <michael.goffioul@gmail.com> * plot/print.m: Properly set the default ghostscript_binary
--- a/scripts/miscellaneous/getfield.m +++ b/scripts/miscellaneous/getfield.m @@ -1,4 +1,5 @@ ## Copyright (C) 2000, 2006, 2007, 2009 Etienne Grossmann +## Copyright (C) 2009 VZLU Prague ## ## This file is part of Octave. ## @@ -42,17 +43,19 @@ ## Author: Etienne Grossmann <etienne@cs.uky.edu> -function s = getfield (s, varargin) - - for idx = 1:nargin-1 - i = varargin{idx}; - if (iscell (i)) - s = s(i{:}); - else - s = s.(i); - endif - endfor - +function obj = getfield (s, varargin) + if (nargin < 2) + print_usage (); + endif + subs = varargin; + flds = cellfun (@ischar, subs); + idxs = cellfun (@iscell, subs); + if (all (flds | idxs)) + typs = merge (flds, {"."}, {"()"}); + obj = subsref (s, struct ("type", typs, "subs", subs)); + else + error ("getfield: invalid index"); + endif endfunction %!test
--- a/scripts/miscellaneous/setfield.m +++ b/scripts/miscellaneous/setfield.m @@ -1,4 +1,5 @@ ## Copyright (C) 2000, 2006, 2007, 2009 Etienne Grossmann +## Copyright (C) 2009 VZLU Prague ## ## This file is part of Octave. ## @@ -43,22 +44,19 @@ ## Author: Etienne Grossmann <etienne@cs.uky.edu> function obj = setfield (obj, varargin) - field = "obj"; - for i = 1:nargin-2 - v = varargin{i}; - if (iscell (v)) - sep = "("; - for j = 1:length (v) - field = sprintf ("%s%s%s", field, sep, num2str (v{j})); - sep = ","; - endfor - field = sprintf ("%s)", field); - else - field = sprintf ("%s.%s", field, v); - endif - endfor - val = varargin{nargin-1}; - eval (sprintf ("%s=val;", field)); + if (nargin < 3) + print_usage (); + endif + subs = varargin(1:end-1); + rhs = varargin{end}; + flds = cellfun (@ischar, subs); + idxs = cellfun (@iscell, subs); + if (all (flds | idxs)) + typs = merge (flds, {"."}, {"()"}); + obj = subsasgn (obj, struct ("type", typs, "subs", subs), rhs); + else + error ("setfield: invalid index"); + endif endfunction %!test