Not enough arguments error when I clearly supply enough arguments?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Mason Scott
am 22 Jul. 2015
Bearbeitet: Stephen23
am 22 Jul. 2015
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 Kommentare
Akzeptierte Antwort
Stephen23
am 22 Jul. 2015
Bearbeitet: Stephen23
am 22 Jul. 2015
[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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Structures 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!