I am trying to write a functin addmany.m that takes zero or more numbers as a variable number of input arguments and returns the sum of its input arguments. I am trying to use the narargin and varargin commands. I have the following code written:
function [y] = main(varargin)%This function will take zero or more numbers as a variable number of input
%arguments and returns the sum of its input arguments.
switch nargin case 0 disp('0 inputs given') alpha = 10 %default value
beta = 10 %default value case 1 disp('1 inputs given') alpha = varargin{1} beta = 10 %default value case 2 disp('2 inputs given') alpha = varargin{1} beta = varargin{2} otherwise error('Unexpected inputs')endy= alpha + beta;
I am trying to get the following result Input is 1,10,100 Output is 111.
If you could please tell me which line/part is wrong and what it should be, I would appreciate it
Best Answer