Undeclared function or variable.

4 次查看(过去 30 天)
When I run the function (given below), it shows an error:
" Undefined function or variable 'class_pointb'"
(The main code where the function is called is also attached. )
function counter = check(edge,point,n)
count_edge = zeros(1,10,4);
fit = 4;
interval = n / 10;
div = point(1,1) / interval;
for i = 1:10
if div >= i && div < i + 1
class_pointa = i;
break
end
end
div = point(1,2) / interval;
for i = 1:10
if div >= i && div < i + 1
class_pointb = i;
end
end
if edge(1) == 1
count_edge(1,class_pointa,1) = count_edge(1,class_pointa,1) + 1;
a = 1;
end
if edge(2) == 1
count_edge(1,class_pointb,1) = count_edge(1,class_pointb,1) + 1;
b = 1;
end
if count_edge(1,class_pointa,a) < fit && count_edge(1,class_pointb,b) < fit
counter = 1;
else
counter = 0;
end
end
I do not understand this because i think i have defined it as : class_pointb = i; How can it be corrected?

采纳的回答

KL
KL 2017-12-17
Your condition div >= i && div < i + 1 probably never becomes true and so class_pointb is never defined. This is why initializing is good (with a zero maybe) and then you'll have to handle this value inside your count_edge function.

更多回答(1 个)

Jan
Jan 2017-12-17
编辑:Jan 2017-12-17
You can check this by using the debugger: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html . Set a breakpoint in "class_pointa = i;" line and run the code again. You will see, that the condition is not met, as K L has explained already.
Using the debugger to examine code is essential for programming. It is even more efficient than asking the forum.
By the way: The loop might be less efficient then a vectorized approach:
% for i = 1:10
% if div >= i && div < i + 1
% class_pointa = i;
% break
% end
% end
% This can reply [] !!!
class_pointa = floor(div(div >=1 & div < 11));

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by