{"id":180,"date":"2009-04-14T18:12:26","date_gmt":"2009-04-14T18:12:26","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/04\/14\/convenient-nargout-behavior\/"},"modified":"2009-04-09T19:09:28","modified_gmt":"2009-04-09T19:09:28","slug":"convenient-nargout-behavior","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/04\/14\/convenient-nargout-behavior\/","title":{"rendered":"Convenient nargout Behavior"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Here's a question that was recently posed at MathWorks.<\/p>\r\n      <p><html> <blockquote> I'm trying to invoke a function handle.  It either returns 0 or 1 output arguments. If it returns an output\r\n         argument, I want to get it; if not I would like to just invoke the function and be on my way.  I have no way of knowing ahead\r\n         of time whether the function returns something or not. <\/blockquote> <\/html>\r\n      <\/p><pre> <\/pre><p>This situation can arise when you are building an application in which you expect to work with user-supplied functions.  You\r\n         can solve this using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/try.html\"><tt>try<\/tt><\/a> construct, but this situation arises frequently enough that we decided to introduce the <tt>nargout<\/tt> bump. So what happens?\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">nargout Bump from 0 to 1<\/a><\/li>\r\n         <li><a href=\"#3\">Why does nargout Behave this Way?<\/a><\/li>\r\n         <li><a href=\"#4\">Will this Simplify Some Code?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>nargout Bump from 0 to 1<a name=\"1\"><\/a><\/h3>\r\n   <p>Many MATLAB functions return 1 result even when called with an <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/nargout.html\"><tt>nargout<\/tt><\/a> is 0. For these, here's a technique that may be of interest.  It allows for a bump in <tt>nargout<\/tt> on the left-hand side.\r\n   <\/p>\r\n   <p>Let's assume the variable <tt>c<\/tt> is unitialized and then call a function <tt>f<\/tt>.\r\n   <\/p><pre>   [c{1:0}] = f(...);<\/pre><p><tt>f<\/tt> is called with <tt>nargout<\/tt> of 0, because <tt>1:0<\/tt> is an empty vector.  If <tt>f<\/tt> returns no arguments, <tt>c<\/tt> is an empty <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/cell.html\"><tt>cell<\/tt><\/a>. <i>However<\/i>, if <tt>f<\/tt> returns a result, <tt>c<\/tt> is a 1x1 cell containing that result.\r\n   <\/p>\r\n   <h3>Why does nargout Behave this Way?<a name=\"3\"><\/a><\/h3>\r\n   <p>Since this is not a widely known technique, I'll explain one more use of it.  Suppose you are writing a function <tt>foo<\/tt>, and after some preparation you want <tt>foo<\/tt> to return all of the results, if any, of a call on <tt>bar<\/tt>, passing along to <tt>bar<\/tt> the number of arguments given to <tt>foo<\/tt>.  The template looks like this.\r\n   <\/p><pre>   function varargout = foo(...)\r\n     ... whatever ...\r\n   [varargout{1:nargout}] = bar(...);<\/pre><p>Anonymous functions use exactly this technique to pass along <tt>nargout<\/tt> to their body and to return that body's results as the result of the anonymous function.  This requirement on anonymous functions,\r\n      and the observation that a significant number of other functions work in the same way, is why we introduced the bump.\r\n   <\/p>\r\n   <h3>Will this Simplify Some Code?<a name=\"4\"><\/a><\/h3>\r\n   <p>I wonder if this information allows you to simplify some of your code. Please share your results <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=180#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_28d458c72e144bcba347155a8023e5e9() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='28d458c72e144bcba347155a8023e5e9 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 28d458c72e144bcba347155a8023e5e9';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2009 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_28d458c72e144bcba347155a8023e5e9()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.8<br><\/p>\r\n<\/div>\r\n<!--\r\n28d458c72e144bcba347155a8023e5e9 ##### SOURCE BEGIN #####\r\n%% Convenient nargout Behavior\r\n% Here's a question that was recently posed at MathWorks.\r\n%\r\n% <html>\r\n% <blockquote>\r\n% I'm trying to invoke a function handle.  It either returns 0 or 1 output\r\n% arguments.\r\n% If it returns an output argument, I want to get it; if not I would like \r\n% to just invoke the function and be on my way.  I have no way\r\n% of knowing ahead of time whether the function returns something or not.\r\n% <\/blockquote>\r\n% <\/html>\r\n%\r\n%  .\r\n%\r\n% This situation can arise when you are building an application in which\r\n% you expect to work with user-supplied functions.  You can solve this\r\n% using <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/try.html |try|>\r\n% construct, but this situation arises frequently enough that we decided to\r\n% introduce the |nargout| bump. So what happens?\r\n%% nargout Bump from 0 to 1\r\n% Many MATLAB functions return 1 result even when called with an \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/nargout.html |nargout|>\r\n% is 0. For these, here's a technique that may be of interest.  It allows\r\n% for a bump in |nargout| on the left-hand side.\r\n%%\r\n% Let's assume the variable |c| is unitialized and then call a function |f|.\r\n% \r\n%     [c{1:0}] = f(...);\r\n% \r\n% |f| is called with |nargout| of 0, because |1:0| is an empty vector.  If\r\n% |f| returns no arguments, |c| is an empty \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/cell.html |cell|>.\r\n% _However_, if |f| returns a result, |c| is a 1x1 cell containing that \r\n% result.\r\n%% Why does nargout Behave this Way?\r\n% Since this is not a widely known technique, I'll explain one more use of\r\n% it.  Suppose you are writing a function |foo|, and after some preparation\r\n% you want |foo| to return all of the results, if any, of a call on |bar|,\r\n% passing along to |bar| the number of arguments given to |foo|.  The\r\n% template looks like this.\r\n% \r\n%     function varargout = foo(...)\r\n%       ... whatever ...\r\n%     [varargout{1:nargout}] = bar(...);\r\n%\r\n% Anonymous functions use exactly this technique to pass along |nargout| to\r\n% their body and to return that body's results as the result of the\r\n% anonymous function.  This requirement on anonymous functions, and the\r\n% observation that a significant number of other functions work in the same\r\n% way, is why we introduced the bump.\r\n%% Will this Simplify Some Code?\r\n% I wonder if this information allows you to simplify some of your code.\r\n% Please share your results <https:\/\/blogs.mathworks.com\/loren\/?p=180#respond here>.\r\n\r\n##### SOURCE END ##### 28d458c72e144bcba347155a8023e5e9\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Here's a question that was recently posed at MathWorks.\r\n        I'm trying to invoke a function handle.  It either returns 0 or 1 output arguments. If it returns an output\r\n   ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/04\/14\/convenient-nargout-behavior\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,15,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/180"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=180"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/180\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}