Not enough arguments error when I clearly supply enough arguments?
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Stephen23
2015-7-22
编辑:Stephen23
2015-7-22
[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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!