Search code examples
matlabsymbolic-math

How to handle multiple inputs while writing a matlab function


How to handle multiple inputs while writing a matlab function ? For example if n is the number of parameters to be passed in the run time then how do my function prototype will look ? Any help will be good. Thanks.


Solution

  • An example of function with various number of parameters is:

    function my_function(varargin)
    
    % This is an example
    min_arg = 1;
    max_arg = 6;
    
    % Check numbers of arguments
    error(nargchk(min_arg,max_arg,nargin))
    
    % Check number of arguments and provide missing values
    if nargin==1
        w = 80;
    end
    
    % Your code ...
    
    end
    

    The same is for the output.