MATLAB Answers

# Undeclared function or variable.

13 views (last 30 days)
Khushank Singhal on 17 Dec 2017
Edited: Jan on 17 Dec 2017
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?

#### 0 Comments

Sign in to comment.

### Accepted Answer

KL on 17 Dec 2017
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.

#### 0 Comments

Sign in to comment.

### More Answers (1)

Jan on 17 Dec 2017
Edited: Jan on 17 Dec 2017
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));

#### 0 Comments

Sign in to comment.

Sign in to answer this question.