# HG changeset patch # User Jason Riedy # Date 1233027809 18000 # Node ID 756b0ba61350998cce4540634476e1292b9109ed # Parent 4e39b00218d305b6ecbb18703b696430095c1f78 orderfields.m: avoid loop for non-empty cases. New tests. diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,7 @@ +2009-01-26 Jason Riedy + + * miscellaneous/orderfields.m: Also avoid loop for non-empty structs. + 2009-01-17 Jaroslav Hajek * optimization/fsolve.m: Disable Broyden updates for sparse jacobians. diff --git a/scripts/miscellaneous/orderfields.m b/scripts/miscellaneous/orderfields.m --- a/scripts/miscellaneous/orderfields.m +++ b/scripts/miscellaneous/orderfields.m @@ -87,16 +87,26 @@ endif ## Permute the names in the structure. + args = cell (1, 2 * numel (names)); + args(1:2:end) = names; 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 + args(2:2:end) = {s1.(names)}; endif + t = struct (args{:}); endfunction + +%!shared a, b +%! a = struct ("foo", {1, 2}, "bar", {3, 4}); +%! b = struct ("bar", 6, "foo", 5); +%!test +%! a(2) = orderfields (b, a); +%! assert (a(2).foo, 5) +%!test +%! a(2) = orderfields (b, [2 1]); +%! assert (a(2).foo, 5) +%!test +%! a(2) = orderfields (b, fieldnames (a)); +%! assert (a(2).foo, 5)