diff scripts/miscellaneous/getfield.m @ 9849:87fd803c583b

rewrite getfield and setfield
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 20 Nov 2009 14:46:13 +0100
parents 1bf0ce0930be
children 9d1a14e12431
line wrap: on
line diff
--- a/scripts/miscellaneous/getfield.m	Fri Nov 20 14:43:38 2009 +0100
+++ b/scripts/miscellaneous/getfield.m	Fri Nov 20 14:46:13 2009 +0100
@@ -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