Comments on: Multiple Outputs https://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/?s_tid=feedtopost Loren Shure is interested in the design of the MATLAB language. She is an application engineer and writes here about MATLAB programming and related topics. Sat, 28 Jan 2012 20:15:37 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Brad Stiritz https://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32941 Sat, 28 Jan 2012 20:15:37 +0000 https://blogs.mathworks.com/loren/?p=75#comment-32941 @C Rose : thanks for your clear argument in favor of returning a single struct with multiple fields, rather than returning a vector of multiple return values.

I’m working on a complex “function stack” at the moment, of several very similar, complex function signatures. I reached a sort-of emotional tipping point, where the cognitive cost of keeping track of slightly different multiple-return-value vectors as I read & step through & maintain this code just isn’t worth the effort anymore. It’s so much more readable to have a canonical form:

stOutput = function_xxx(…)

That said, there’s no question that in many simpler contexts, such as utility functions, it’s highly convenient & value-added to return a single, principal output value & one or more “trailing, optional values”.

So I don’t think this choice is cut-and-dried, but I do agree that students should be taught to keep the trade-off in mind & to go with struct output when they feel things are getting “too complex”.

brad

]]>
By: Loren https://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32326 Thu, 16 Jun 2011 16:42:43 +0000 https://blogs.mathworks.com/loren/?p=75#comment-32326 Keith-

You can’t have an assignment inside anonymous function definition.

Try something like:

enum = @(x)num2cell(x);

Then call it like this:

c = enum([1 2 3])

Not sure what you are doing with nargout, but you need it to used with a function that has more than one output.

Anonymous functions CAN have multiple outputs. Try using deal in one, for example.

enum = @(x)deal(x{:});
x = { 1 2 3};
[a,b,c] = enum(x)

–Loren

]]>
By: Keith https://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32325 Thu, 16 Jun 2011 16:23:52 +0000 https://blogs.mathworks.com/loren/?p=75#comment-32325 Hi.

I am trying to make some dumb function anonymous. The following is easily put in an m-file and does what I want:

function varargout = enumerate()
varargout = num2cell(1:nargout);

Since it’s just a single line, I figure it would work, but it generates the error:

>> enum = @() varargout = num2cell(1:nargout);
??? enum = @() varargout = num2cell(1:nargout);
                         |
Error: The expression to the left of the equals sign is not a valid target for
an assignment.

I guess it doesn’t work because anonymous functions can’t have variable numbers of outputs, but is there some trick I haven’t thought of to accomplishing the same task anonymously?

]]>
By: Doug Weathers https://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32172 Fri, 01 Apr 2011 23:22:32 +0000 https://blogs.mathworks.com/loren/?p=75#comment-32172 I got a reply from Support, explaining why it doesn’t work. However, the tech included a piece of code that contained the seed of a workaround.

Instead of using

events = @(t,y) deal(norm(y)-1, 1, 1);

use

mydeal = @(x) x{:};
evt = @(t,y) mydeal({norm(y)-1, 1, 1});

and all is well.

I still think that DEAL is broken, though. Lots of other people complain about this inflexibility of DEAL, as I discovered while researching this issue. Loren seemed to think so in 2007, when she wrote the original post!

Thanks everyone!

– Doug

]]>
By: Doug Weathers https://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32167 Thu, 31 Mar 2011 20:23:09 +0000 https://blogs.mathworks.com/loren/?p=75#comment-32167 @Loren,

I posted a bug report. Thanks!

@jiro,

I did some debugging with “step in” and saw the anonymous event function fail a test to see if nargin == nargout. I think. I’ve slept since then.

]]>
By: jiro https://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32163 Wed, 30 Mar 2011 12:01:40 +0000 https://blogs.mathworks.com/loren/?p=75#comment-32163 @Doug,
It seems like you’re looking at the issue itself in one of the error messages:

Error in ==> odeevents at 29
  eventValue = feval(eventFcn,t0,y0,eventArgs{:});
 

That command is only requesting one output, as you suspect. In another place in the code, it is requesting all 3.

]]>
By: Doug Weathers https://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32161 Wed, 30 Mar 2011 06:16:23 +0000 https://blogs.mathworks.com/loren/?p=75#comment-32161 Hi Loren,

Thanks for the tip on using DEAL to produce inline anonymous functions that produce multiple outputs.

However, it doesn’t work in the case of an event function passed to an ODE solver:

tilde = @(q) [   0  -q(3)  q(2)
               q(3)    0  -q(1)
              -q(2)  q(1)    0  ];
sigma0 = [0 0 0];
omega = [1, 0.5, -0.7]';
I = eye(3);
sdot = @(t, s) 1/4 * ((1-dot(s,s))*I + 2*tilde(s) + 2*s*s') * omega;
t = [0 5];
events = @(t,y) deal(norm(y)-1, 1, 1);
options = odeset('Events', events);
[tevent, sevent] = ode45(sdot, t, sigma0, options);


??? Error using ==> deal at 38
The number of outputs should match the number of inputs.

Error in ==> @(t,y)deal(norm(y)-1,1,1)


Error in ==> odeevents at 29
  eventValue = feval(eventFcn,t0,y0,eventArgs{:});

Error in ==> ode45 at 207
[haveEventFcn,eventFcn,eventArgs,valt,teout,yeout,ieout] = ...

Error in ==> example at 11
[tevent, sevent] = ode45(sdot, t, sigma0, options);
 

It looks like somewhere along the line, ODE45 is trying to get something other than three outputs from my event function, and DEAL complains.

Some desultory debugging seems to indicate that my anonymous events function is advertising that its nargout is only 1, which is a DEAL-breaker.

It works fine if I use an external function file (as is usually done) as my events function.

Any thoughts?

Thanks,

Doug

]]>
By: Loren https://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32162 Wed, 30 Mar 2011 05:44:33 +0000 https://blogs.mathworks.com/loren/?p=75#comment-32162 Doug-

I don’t know for sure and don’t have access to MATLAB right now. I think you should contact support to identify where the problem lies.

–Loren

]]>
By: Tristan https://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-31916 Wed, 15 Dec 2010 11:03:44 +0000 https://blogs.mathworks.com/loren/?p=75#comment-31916 This was very helpful, did not know about the

deal

function!

]]>
By: Joan https://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-31626 Wed, 01 Sep 2010 20:26:30 +0000 https://blogs.mathworks.com/loren/?p=75#comment-31626 Hi Loren, thanks for this. It looks really neat — and it looks just standard plain Matlab… maybe next time I can come with a cleverer question!
Joan

]]>