Stuart’s MATLAB Videos

Watch and Learn

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Managing code in MATLAB: Functions of variable numbers of inputs and outputs 4

Posted by Doug Hull,

This is the first series of MATLAB video tutorials that are an attempt to systematically make videos for a “technology trees” for using MATLAB. This first tree covers the various kinds of MATLAB files you might write. The tree shows you the order you should watch them because some rely on skills you need to learn from earlier videos, but some do not.

Script Function 1in1out MinNout VarArg Cell mode Markup Interactive Driver script Sub function Nested

This video will allow you to do input parsing so that you have the ability to overload functions, meaning allow them to react differently to different inputs.

4 CommentsOldest to Newest

Peter replied on : 1 of 4
You didn't show the fun case where you type "main()" and it prints "0" because you requested no outputs. Explain how that makes any sense. How does it produce an output 0 if it didn't have any outputs to produce?
Doug replied on : 2 of 4
Peter, I think you are referring to this case: function varargout = main(varargin) varargout{1} = nargout; varargout{2} = nargin; ------- >>main ans = 0 -------------- Within the context of the function itself, it sees that there were no *requested* outputs. That is indeed correct within that context. From the outside, if there are no *requested* outputs the variable ans will be the assumed left hand variable to be assigned the default one output case. What would you expect to happen here? Self-referential logical can lead to paradoxes: "This statement is false." -Doug
Doug replied on : 3 of 4
Peter, I think you are referring to this case: function varargout = main(varargin) varargout{1} = nargout; varargout{2} = nargin; ------- >>main ans = 0 -------------- Within the context of the function itself, it sees that there were no *requested* outputs. That is indeed correct within that context. From the outside, if there are no *requested* outputs the variable ans will be the assumed left hand variable to be assigned the default one output case. What would you expect to happen here? Self-referential stements can lead to paradoxes: "This statement is false." -Doug
Doug replied on : 4 of 4
Peter, I think you are referring to this case: function varargout = main(varargin) varargout{1} = nargout; varargout{2} = nargin; ------- >>main ans = 0 -------------- Within the context of the function itself, it sees that there were no *requested* outputs. That is indeed correct within that context. From the outside, if there are no *requested* outputs the variable ans will be the assumed left hand variable to be assigned the default one output case. What would you expect to happen here? Self-referential statements can lead to paradoxes: "This statement is false." -Doug

Add A Comment

Your email address will not be published. Required fields are marked *

Preview: hide