an error received while executing my function?

36 views Asked by At

When I execute the following:

function [ x ] = addya( varargin )
x=varargin{1};
t=varargin{1};
if(nargin>1)

for i=2:nargin
    t=t+varargin(i);
    end;
end
x=t;

The error i am getting is:

addya(1,1) ??? Undefined function or method 'addya' for input arguments of type 'double'.

please suggest changes and errors.

1

There are 1 answers

2
Dev-iL On

Make sure this function is saved in a file called addya.m.

Moreover, as mentioned by il_raffa there's a typo - inside the loop: you should access varargin using {}.

The following code works for me when saved as addya.m:

function [ x ] = addya( varargin )
    x=varargin{1}; %// Why is this needed?
    t=varargin{1};
    if(nargin>1)
        for i=2:nargin
            t=t+varargin{i};
        end;
    end
    x=t;

Also, I would suggest to refrain from using i as a loop index due to possible complication with complex numbers.