matlab varargin和varargout混合使用

代码

function varargout=definedAndVariableNumInputs(X,Y,varargin)
    disp("Total number of input arguments: " + nargin)
    
    formatSpec = "Size of varargin cell array: %dx%d";
    str = compose(formatSpec,size(varargin));
    disp(str)
    
    if nargout==1
        varargout={X};
    elseif nargout==2
        varargout={X,Y};
    else
        error('错误');
    
    end

end

输出

>> a=definedAndVariableNumInputs(7,pi,rand(4),datetime('now'),'hello')
Total number of input arguments: 5
Size of varargin cell array: 1x3

a =

     7

>> [a,b]=definedAndVariableNumInputs(7,pi,rand(4),datetime('now'),'hello')
Total number of input arguments: 5
Size of varargin cell array: 1x3

a =

     7


b =

    3.1416

>> [a,b,c]=definedAndVariableNumInputs(7,pi,rand(4),datetime('now'),'hello')
Total number of input arguments: 5
Size of varargin cell array: 1x3
错误使用 definedAndVariableNumInputs (line 13)
错误
 
>> 

你可能感兴趣的:(matlab varargin和varargout混合使用)