changeset 6862:d63339cbb205

[project @ 2007-09-05 06:49:29 by jwe]
author jwe
date Wed, 05 Sep 2007 06:49:29 +0000
parents 32558239ed9e
children 3c64128e621c
files scripts/ChangeLog scripts/miscellaneous/orderfields.m
diffstat 2 files changed, 23 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Wed Sep 05 06:15:01 2007 +0000
+++ b/scripts/ChangeLog	Wed Sep 05 06:49:29 2007 +0000
@@ -1,3 +1,11 @@
+2007-09-05  John W. Eaton  <jwe@octave.org>
+
+	* miscellaneous/orderfields.m: Use numel instead of length.
+
+2007-09-05  Michael goffioul  <michael.goffioul@gmail.com>
+
+	* miscellaneous/orderfields.m: Handle empty structs.
+
 2007-09-05  John W. Eaton  <jwe@octave.org>
 
 	* plot/__go_draw_axes__.m: Consistently index PARAMETRIC with
--- a/scripts/miscellaneous/orderfields.m	Wed Sep 05 06:15:01 2007 +0000
+++ b/scripts/miscellaneous/orderfields.m	Wed Sep 05 06:49:29 2007 +0000
@@ -46,11 +46,11 @@
 
   if (nargin == 1)
     ## One structure: return the fields in alphabetical order.
-    if (isstruct(s1))
+    if (isstruct (s1))
       names = sort (fieldnames (s1));
     endif
   elseif (nargin == 2)
-    if (isstruct(s2))
+    if (isstruct (s2))
       ## Two structures: return the fields in the order of s2.
       names = fieldnames (s2);
       if (! isequal (sort (fieldnames (s1)), sort (names)))
@@ -69,11 +69,11 @@
       names = fieldnames (s1);
       t1 = sort (s2);
       t1 = t1(:)';
-      t2 = 1:length (names);
+      t2 = 1:numel (names);
       if (! isequal (t1, t2))
 	error ("orderfields: invalid permutation vector");
       endif
-      names = names(s2);
+      names = names (s2);
     endif
   endif
 
@@ -88,9 +88,16 @@
   endif
 
   ## Permute the names in the structure.
-  for i = 1:length (names)
-    el = names(i);
-    t(:).(el) = s1(:).(el);
-  endfor
+  if (numel (s1) == 0)
+    args = cell (1, 2 * numel (names));
+    args(1:2:end) = names;
+      args{2:2:end} = {};
+    t = struct (args{:});
+  else
+    for i = 1:numel (names)
+      el = names(i);
+      t(:).(el) = s1(:).(el);
+    endfor
+  endif
 
 endfunction