Undeclared function or variable.

5 ビュー (過去 30 日間)
Khushank Singhal
Khushank Singhal 2017 年 12 月 17 日
編集済み: Jan 2017 年 12 月 17 日
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 Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by