[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: structfun: more than one output argument
From: |
Thorsten Meyer |
Subject: |
Re: structfun: more than one output argument |
Date: |
Mon, 18 Jan 2010 22:53:20 +0100 |
User-agent: |
Mozilla-Thunderbird 2.0.0.22 (X11/20090706) |
Hi
John W. Eaton wrote:
> On 17-Jan-2010, Thorsten Meyer wrote:
>
> | here is a patch to extend the capability of cellfun of handling more than
> one
> | output argument to structfun. This will make structfun more matlab
> compatible.
>
> Maybe I'm missing something, but the new test you added already passes
> without the change. So can you send an example of something that
> should work but fails without your patch?
>
You have been quite right (and there still was a typo in the code).
structfun has already been able to handle multiple output arguments with
the (default) "uniformoutput", true option - but not for
"uniformoutput", false.
I attach an updated patch including a third test that really fails
without the patch:
lt-octave:1> s = struct ("a", {1, 2, 3}, "b", {4, 5, 6});
lt-octave:2> c = struct ("a", {2, 4, 6}, "b", {8, 10, 12});
lt-octave:3> d = struct ("a", {1, 4, 9}, "b", {16, 25, 36});
lt-octave:4> function [a, b] = twoouts (x)
> a = x + x;
> b = x * x;
> endfunction
lt-octave:5> [aa, bb] = structfun(@twoouts, s, "uniformoutput", false);
error: some elements undefined in return list
error: called from:
error: /home/thorsten/hg/octave/scripts/general/structfun.m at line
81, column 21
(I left the other new test in to test with "uniformoutput", true also)
Regards
Thorsten
# HG changeset patch
# User Thorsten Meyer <address@hidden>
# Date 1263851211 -3600
# Node ID 433e385b4a4b510d9664a36af1d7abbb45efa742
# Parent ab80681c44d9cc1c3cb82689260690a9f1a2c48b
[mq]: structfun_more_output_args
diff -r ab80681c44d9 -r 433e385b4a4b scripts/ChangeLog
--- a/scripts/ChangeLog Mon Jan 18 14:14:08 2010 +0100
+++ b/scripts/ChangeLog Mon Jan 18 22:46:51 2010 +0100
@@ -0,0 +1,5 @@
+2010-01-18 Thorsten Meyer <address@hidden>
+
+ * general/structfun.m: Add support for more than one output
+ argument in case of "uniformoutput", false.
+
diff -r ab80681c44d9 -r 433e385b4a4b scripts/general/structfun.m
--- a/scripts/general/structfun.m Mon Jan 18 14:14:08 2010 +0100
+++ b/scripts/general/structfun.m Mon Jan 18 22:46:51 2010 +0100
@@ -78,7 +78,9 @@
[varargout{:}] = cellfun (fun, struct2cell (s), varargin{:});
if (iscell (varargout{1}))
- [varargout{:}] = cell2struct (varargout{1}, fieldnames(s), 1);
+ for n = 1:length(varargout)
+ varargout{n} = cell2struct (varargout{n}, fieldnames(s), 1);
+ endfor
endif
endfunction
@@ -91,3 +93,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);