How to take variable number of inputs arugments and return their sum
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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')
end
y= 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
Accepted Answer
Image Analyst
on 29 Oct 2013
Try this:
function theSum = AddMany(varargin)
theSum = []; % Initialize
if nargin == 0
uiwait(warndlg('Please input 1 or more numbers'));
return;
end
fprintf('Number of arguments: %d\n',nargin);
celldisp(varargin)
inputNumbers = cell2mat(deal(varargin))
theSum = sum(inputNumbers);
13 Comments
Image Analyst
on 29 Oct 2013
You had a function name of main() and a filename of addmany.m, which is a conflict and not recommended.
KayLynn
on 29 Oct 2013
I follow exactly what you are doing here execpt...is there a way to use something else in place of uiwait(warndlg)...I was thinking something like fprintf but wasnt sure how to write that out.
Daniel Shub
on 29 Oct 2013
+1 for celldisp
KayLynn
on 29 Oct 2013
daniel.....are you saying to replace the uiwait command with +1 cell disp or are you just saying to replace varargin with +1cell disp or neither of these
Daniel Shub
on 29 Oct 2013
Why not just
inputNumbers = [varargin{:}]
Neither. He's saying he voted for ImageAnalyst's answer (thereby awarding him +1 reputation points), because he likes how celldisp was used.
KayLynn
on 29 Oct 2013
Still not sure how to remove the uiwait(warndlg) line and replace it with something diffierent..(if that is at all possible)
Daniel Shub
on 29 Oct 2013
@KayLynn, you probably want to merge your example main function which has default values with IA's answer which doesn't have default values. Once you understand how both functions work, it should be rather trivial to merge them.
Matt J
on 29 Oct 2013
"Something different" is a broad request. How about
if ~nargin
error 'Please input 1 or more numbers'
end
KayLynn
on 29 Oct 2013
Thank you all for your help. I have my function up and executing correctly now.
Image Analyst
on 29 Oct 2013
Edited: Image Analyst
on 29 Oct 2013
Daniel, inputNumbers = [varargin{:}] gives a cell array, and the sum() function is not defined for cell arrays. So you'd still have to use cell2mat. Though it does look like the brackets perform the same as deal(), at least in this instance.
Daniel Shub
on 29 Oct 2013
I am pretty sure with squiggly brackets {} you get an array of doubles and with smooth brackets () you get a cell array. So with {} it seems to work
x = {1, 2, 3};
sum([x{:}])
Image Analyst
on 29 Oct 2013
You're right - I forgot the braces when I tested it.
More Answers (0)
Categories
Find more on Argument Definitions in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)