passing arguments to a function

1 Ansicht (letzte 30 Tage)
Deepa Maheshvare
Deepa Maheshvare am 29 Aug. 2020
Beantwortet: Hussein Ammar am 29 Aug. 2020
function val = sum(a,b)
default('a',1); % uses a function from file exchange
default('b',20);
val = a+b
end
I want to pass only the second argument and use the default for first,a. Something like below,
val = sum(b=2)
How can I do this in MATLAB?

Akzeptierte Antwort

Hussein Ammar
Hussein Ammar am 29 Aug. 2020
One way of doing this is to pass an empty argument, e.g., mySum([], 2) or mySum(2, []). So, you can add the following conditions:
function myVal = mySum(a, b)
if isempty(a)
a = 1;
end
if isempty(b)
b = 20;
end
myVal = a+b;
end

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by