Function with variable number of input arguments
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sid jayadev
am 24 Jul. 2014
Kommentiert: Sid jayadev
am 24 Jul. 2014
Hey everyone,
I created a function called matrixmultiply, and I'm trying to define it for 2, 3, and 4 input arguments. How do I do this in one single script? I've heard "varargin" and "nargin" variables could work, but I don't know how to use those.
(I've defined it separately for 4 arguments already.)
Thanks!
0 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 24 Jul. 2014
In your case you can just use nargin to know the number of inputs. Look at this example
function y=fcn10(a,b,c,d)
if nargin==2
y=a+b
elseif nargin==3
y=a*b*c
elseif nargin==4
y=(a+b)*(c+d)
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Argument Definitions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!