<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Multiple Outputs</title>
	<atom:link href="http://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Brad Stiritz</title>
		<link>http://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32941</link>
		<dc:creator>Brad Stiritz</dc:creator>
		<pubDate>Sat, 28 Jan 2012 20:15:37 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=75#comment-32941</guid>
		<description>@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&#039;m working on a complex &quot;function stack&quot; 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 &amp; step through &amp; maintain this code just isn&#039;t worth the effort anymore. It&#039;s so much more readable to have a canonical form:


stOutput = function_xxx(...)


That said, there&#039;s no question that in many simpler contexts, such as utility functions, it&#039;s highly convenient &amp; value-added to return a single, principal output value &amp; one or more &quot;trailing, optional values&quot;.

So I don&#039;t think this choice is cut-and-dried, but I do agree that students should be taught to keep the trade-off in mind &amp; to go with struct output when they feel things are getting &quot;too complex&quot;.

brad</description>
		<content:encoded><![CDATA[<p>@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.</p>
<p>I&#8217;m working on a complex &#8220;function stack&#8221; 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 &amp; step through &amp; maintain this code just isn&#8217;t worth the effort anymore. It&#8217;s so much more readable to have a canonical form:</p>
<p>stOutput = function_xxx(&#8230;)</p>
<p>That said, there&#8217;s no question that in many simpler contexts, such as utility functions, it&#8217;s highly convenient &amp; value-added to return a single, principal output value &amp; one or more &#8220;trailing, optional values&#8221;.</p>
<p>So I don&#8217;t think this choice is cut-and-dried, but I do agree that students should be taught to keep the trade-off in mind &amp; to go with struct output when they feel things are getting &#8220;too complex&#8221;.</p>
<p>brad</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32326</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 16 Jun 2011 16:42:43 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=75#comment-32326</guid>
		<description>Keith-

You can&#039;t have an assignment inside anonymous function definition.

Try something like:

&lt;pre class=&quot;code&quot;&gt;
enum = @(x)num2cell(x);
&lt;/pre&gt;

Then call it like this:

&lt;pre class=&quot;code&quot;&gt;
c = enum([1 2 3])
&lt;/pre&gt;

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.

&lt;pre class=&quot;code&quot;&gt;
enum = @(x)deal(x{:});
x = { 1 2 3};
[a,b,c] = enum(x)
&lt;/pre&gt;

--Loren</description>
		<content:encoded><![CDATA[<p>Keith-</p>
<p>You can&#8217;t have an assignment inside anonymous function definition.</p>
<p>Try something like:</p>
<pre class="code">
enum = @(x)num2cell(x);
</pre>
<p>Then call it like this:</p>
<pre class="code">
c = enum([1 2 3])
</pre>
<p>Not sure what you are doing with nargout, but you need it to used with a function that has more than one output.</p>
<p>Anonymous functions CAN have multiple outputs.   Try using deal in one, for example.</p>
<pre class="code">
enum = @(x)deal(x{:});
x = { 1 2 3};
[a,b,c] = enum(x)
</pre>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith</title>
		<link>http://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32325</link>
		<dc:creator>Keith</dc:creator>
		<pubDate>Thu, 16 Jun 2011 16:23:52 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=75#comment-32325</guid>
		<description>Hi.

I am trying to make some dumb function anonymous.  The following is easily put in an m-file and does what I want:

&lt;pre&gt;
function varargout = enumerate()
varargout = num2cell(1:nargout);
&lt;/pre&gt;

Since it&#039;s just a single line, I figure it would work, but it generates the error:

&lt;pre&gt;
&gt;&gt; enum = @() varargout = num2cell(1:nargout);
??? enum = @() varargout = num2cell(1:nargout);
                         &#124;
Error: The expression to the left of the equals sign is not a valid target for
an assignment.
&lt;/pre&gt;

I guess it doesn&#039;t work because anonymous functions can&#039;t have variable numbers of outputs, but is there some trick I haven&#039;t thought of to accomplishing the same task anonymously?</description>
		<content:encoded><![CDATA[<p>Hi.</p>
<p>I am trying to make some dumb function anonymous.  The following is easily put in an m-file and does what I want:</p>
<pre>
function varargout = enumerate()
varargout = num2cell(1:nargout);
</pre>
<p>Since it&#8217;s just a single line, I figure it would work, but it generates the error:</p>
<pre>
&gt;&gt; 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.
</pre>
<p>I guess it doesn&#8217;t work because anonymous functions can&#8217;t have variable numbers of outputs, but is there some trick I haven&#8217;t thought of to accomplishing the same task anonymously?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug Weathers</title>
		<link>http://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32172</link>
		<dc:creator>Doug Weathers</dc:creator>
		<pubDate>Fri, 01 Apr 2011 23:22:32 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=75#comment-32172</guid>
		<description>I got a reply from Support, explaining why it doesn&#039;t work.  However, the tech included a piece of code that contained the seed of a workaround.

Instead of using

&lt;pre&gt;events = @(t,y) deal(norm(y)-1, 1, 1);

&lt;/pre&gt;


use

&lt;pre&gt;mydeal = @(x) x{:};
evt = @(t,y) mydeal({norm(y)-1, 1, 1});

&lt;/pre&gt;


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</description>
		<content:encoded><![CDATA[<p>I got a reply from Support, explaining why it doesn&#8217;t work.  However, the tech included a piece of code that contained the seed of a workaround.</p>
<p>Instead of using</p>
<pre>events = @(t,y) deal(norm(y)-1, 1, 1);
</pre>
<p>use</p>
<pre>mydeal = @(x) x{:};
evt = @(t,y) mydeal({norm(y)-1, 1, 1});
</pre>
<p>and all is well.</p>
<p>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!  </p>
<p>Thanks everyone!</p>
<p>- Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug Weathers</title>
		<link>http://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32167</link>
		<dc:creator>Doug Weathers</dc:creator>
		<pubDate>Thu, 31 Mar 2011 15:23:09 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=75#comment-32167</guid>
		<description>@Loren,

I posted a bug report.  Thanks!

@jiro,

I did some debugging with &quot;step in&quot; and saw the anonymous event function fail a test to see if nargin == nargout.  I think.  I&#039;ve slept since then.</description>
		<content:encoded><![CDATA[<p>@Loren,</p>
<p>I posted a bug report.  Thanks!</p>
<p>@jiro,</p>
<p>I did some debugging with &#8220;step in&#8221; and saw the anonymous event function fail a test to see if nargin == nargout.  I think.  I&#8217;ve slept since then.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jiro</title>
		<link>http://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32163</link>
		<dc:creator>jiro</dc:creator>
		<pubDate>Wed, 30 Mar 2011 12:01:40 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=75#comment-32163</guid>
		<description>@Doug,
It seems like you&#039;re looking at the issue itself in one of the error messages:

&lt;pre&gt;
Error in ==&gt; odeevents at 29
  eventValue = feval(eventFcn,t0,y0,eventArgs{:});
 &lt;/pre&gt;

That command is only requesting one output, as you suspect. In another place in the code, it is requesting all 3.</description>
		<content:encoded><![CDATA[<p>@Doug,<br />
It seems like you&#8217;re looking at the issue itself in one of the error messages:</p>
<pre>
Error in ==&gt; odeevents at 29
  eventValue = feval(eventFcn,t0,y0,eventArgs{:});
 </pre>
<p>That command is only requesting one output, as you suspect. In another place in the code, it is requesting all 3.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32162</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 30 Mar 2011 05:44:33 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=75#comment-32162</guid>
		<description>Doug-

I don&#039;t know for sure and don&#039;t have access to MATLAB right now.  I think you should contact support to identify where the problem lies.

--Loren</description>
		<content:encoded><![CDATA[<p>Doug-</p>
<p>I don&#8217;t know for sure and don&#8217;t have access to MATLAB right now.  I think you should contact support to identify where the problem lies.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug Weathers</title>
		<link>http://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-32161</link>
		<dc:creator>Doug Weathers</dc:creator>
		<pubDate>Wed, 30 Mar 2011 01:16:23 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=75#comment-32161</guid>
		<description>Hi Loren,

Thanks for the tip on using DEAL to produce inline anonymous functions that produce multiple outputs.

However, it doesn&#039;t work in the case of an event function passed to an ODE solver:

&lt;pre&gt;
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]&#039;;
I = eye(3);
sdot = @(t, s) 1/4 * ((1-dot(s,s))*I + 2*tilde(s) + 2*s*s&#039;) * omega;
t = [0 5];
events = @(t,y) deal(norm(y)-1, 1, 1);
options = odeset(&#039;Events&#039;, events);
[tevent, sevent] = ode45(sdot, t, sigma0, options);


??? Error using ==&gt; deal at 38
The number of outputs should match the number of inputs.

Error in ==&gt; @(t,y)deal(norm(y)-1,1,1)


Error in ==&gt; odeevents at 29
  eventValue = feval(eventFcn,t0,y0,eventArgs{:});

Error in ==&gt; ode45 at 207
[haveEventFcn,eventFcn,eventArgs,valt,teout,yeout,ieout] = ...

Error in ==&gt; example at 11
[tevent, sevent] = ode45(sdot, t, sigma0, options);
 &lt;/pre&gt;

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</description>
		<content:encoded><![CDATA[<p>Hi Loren,</p>
<p>Thanks for the tip on using DEAL to produce inline anonymous functions that produce multiple outputs.</p>
<p>However, it doesn&#8217;t work in the case of an event function passed to an ODE solver:</p>
<pre>
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 ==&gt; deal at 38
The number of outputs should match the number of inputs.

Error in ==&gt; @(t,y)deal(norm(y)-1,1,1)

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

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

Error in ==&gt; example at 11
[tevent, sevent] = ode45(sdot, t, sigma0, options);
 </pre>
<p>It looks like somewhere along the line, ODE45 is trying to get something other than three outputs from my event function, and DEAL complains.</p>
<p>Some desultory debugging seems to indicate that my anonymous events function is advertising that its nargout is only 1, which is a DEAL-breaker.</p>
<p>It works fine if I use an external function file (as is usually done) as my events function.</p>
<p>Any thoughts?</p>
<p>Thanks,</p>
<p>Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tristan</title>
		<link>http://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-31916</link>
		<dc:creator>Tristan</dc:creator>
		<pubDate>Wed, 15 Dec 2010 11:03:44 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=75#comment-31916</guid>
		<description>This was very helpful, did not know about the &lt;pre&gt;deal&lt;/pre&gt; function!</description>
		<content:encoded><![CDATA[<p>This was very helpful, did not know about the
<pre>deal</pre>
</p><p> function!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joan</title>
		<link>http://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/#comment-31626</link>
		<dc:creator>Joan</dc:creator>
		<pubDate>Wed, 01 Sep 2010 20:26:30 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=75#comment-31626</guid>
		<description>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</description>
		<content:encoded><![CDATA[<p>Hi Loren, thanks for this. It looks really neat &#8212; and it looks just standard plain Matlab&#8230; maybe next time I can come with a cleverer question!<br />
Joan</p>
]]></content:encoded>
	</item>
</channel>
</rss>

