passing arguments to a function

2 ビュー (過去 30 日間)
Deepa Maheshvare
Deepa Maheshvare 2020 年 8 月 29 日
回答済み: Hussein Ammar 2020 年 8 月 29 日
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?

採用された回答

Hussein Ammar
Hussein Ammar 2020 年 8 月 29 日
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

その他の回答 (0 件)

カテゴリ

Find more on Debugging and Analysis in Help Center and File Exchange

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by