Mercurial > hg > octave-lojdl
changeset 10132:aa0f575cf39b
improve structfun's Matlab compatibility
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 19 Jan 2010 10:06:42 +0100 |
parents | 30817aa3889a |
children | 2e4fc7fdba15 |
files | scripts/ChangeLog scripts/general/structfun.m |
diffstat | 2 files changed, 42 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,8 @@ +2010-01-19 Jaroslav Hajek <highegg@gmail.com>, Thorsten Meyer <thorsten.meyier@gmx.de> + + * general/structfun.m: Correctly support multiple arguments with + non-uniform output. Correct test for non-uniform output. + 2010-01-19 Jaroslav Hajek <highegg@gmail.com> * help/print_usage.m: Try determining whether called from top level.
--- a/scripts/general/structfun.m +++ b/scripts/general/structfun.m @@ -1,4 +1,5 @@ ## Copyright (C) 2007, 2008, 2009 David Bateman +## Copyright (C) 2010 VZLU Prague ## ## This file is part of Octave. ## @@ -74,11 +75,21 @@ print_usage (); endif + uniform_output = true; + uo_str = "uniformoutput"; + + nargs = length (varargin); + if (nargs >= 2 && strcmpi (varargin{1}, uo_str)) + uniform_output =varargin{2}; + elseif (nargs >= 4 && strcmpi (varargin{3}, uo_str)) + uniform_output =varargin{4}; + endif + varargout = cell (max ([nargout, 1]), 1); [varargout{:}] = cellfun (fun, struct2cell (s), varargin{:}); - if (iscell (varargout{1})) - [varargout{:}] = cell2struct (varargout{1}, fieldnames(s), 1); + if (! uniform_output) + varargout = cellfun (@cell2struct, varargout, {fieldnames(s)}, {1}, uo_str, false); endif endfunction @@ -91,3 +102,27 @@ %! o = structfun (@(x) regexp (x, '(\w+)$', "matches"){1}, s, %! "UniformOutput", false); %! assert (o, l); + +%!function [a, b] = twoouts (x) +%! a = x + x; +%! b = x * x; + +%!test +%! s = struct ("a", {1, 2, 3}, "b", {4, 5, 6}); +%! c(1:2, 1, 1) = [2; 8]; +%! c(1:2, 1, 2) = [4; 10]; +%! c(1:2, 1, 3) = [6; 12]; +%! d(1:2, 1, 1) = [1; 16]; +%! d(1:2, 1, 2) = [4; 25]; +%! d(1:2, 1, 3) = [9; 36]; +%! [aa, bb] = structfun(@twoouts, s); +%! assert(aa, c); +%! assert(bb, d); + +%!test +%! s = struct ("a", {1, 2, 3}, "b", {4, 5, 6}); +%! c = struct ("a", {2, 4, 6}, "b", {8, 10, 12}); +%! d = struct ("a", {1, 4, 9}, "b", {16, 25, 36}); +%! [aa, bb] = structfun(@twoouts, s, "uniformoutput", false); +%! assert(aa, c); +%! assert(bb, d);