フィルターのクリア

Not enough arguments error when I clearly supply enough arguments?

1 回表示 (過去 30 日間)
Mason Scott
Mason Scott 2015 年 7 月 22 日
編集済み: Stephen23 2015 年 7 月 22 日
I have two functions
function coords = dart_gun01(n)
coords = normrnd(0,10,n,2);
end
and
function [total, tally, shots] = dart_score(n, f_dart)
shots = f_dart(n);
total = 0;
tally = zeros(1,4);
for i=1:n
r = (shots(i,1)^2 + shots(i,2)^2)^0.5;
if r < 1
total = total + 10;
tally(4) = tally(4) + 1;
else if r < 5
total = total + 5;
tally(3) = tally(3) + 1;
else if r < 10
total = total + 1;
tally(2) = tally(2) + 1;
else
tally(1) = tally(1) + 1;
end
end
end
They are meant to work together; the first function is inputted into the second as the argument f_dart. But when I try to call for example:
[total,tally,shots] = dart_score(10,dart_gun01);
I get a not enough arguments error with regards to the dart_gun01 function. I don't why this is happening. for a while is was not working, then it started working, and now it's not working and the whole time I have not changed anything. Why is this?

採用された回答

Stephen23
Stephen23 2015 年 7 月 22 日
編集済み: Stephen23 2015 年 7 月 22 日
Try supplying the supplementary function dart_gun01 as a function handle:
[total,tally,shots] = dart_score(10,@dart_gun01);
Currently what you are doing is this:
[total,tally,shots] = dart_score(10,dart_gun01);
MATLAB looks through the list of input arguments. The first is a numeric value, so no problem there. The second it identifies as a function call, and so it immediately evaluates that function right there, before it even gets started with dart_score. It is exactly as if you call this:
dart_gun01
and you will get the same error: the function is being evaluated and you are not giving it the required arguments. Using a function handle tells MATLAB that you want to pass the function as a variable, and not evaluate it where it appears. More info and examples:

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by