Why "undefined variable" error keeps coming when infact the variable is defined?
12 views (last 30 days)
Show older comments
I have the following code and on line 104 the variable 'Y_Rob' is clearly defined. However Matlab keeps giving the error "Undefined function or variable 'Y_Rob'. Can someone help me to figure this out. Here is the code:
clear; clc;
addpath('Mex');
k2 = Kin2('color','depth');
X_mm_to_pixel = 1190/1920; Y_mm_to_pixel = 670/1080;
redThresh = 0.25;
tc=tcpip('192.168.125.1', 5992);
fopen(tc);
% images sizes
d_width = 512; d_height = 424; outOfRange = 4000;
c_width = 1920; c_height = 1080;
COL_SCALE = 0.5;
% Create matrices for the images
depth = zeros(d_height,d_width,'uint16');
color = zeros(c_height*COL_SCALE,c_width*COL_SCALE,3,'uint8');
% Images used to draw the markers
depthAdditions = zeros(d_height,d_width,3,'uint8');
colorAdditions = zeros(c_height*COL_SCALE,c_width*COL_SCALE,3,'uint8');
% depth stream figure
d.h = figure;
d.ax = axes('units','pixels');
d.im = imshow(depth,[0 255]);
title('Depth Source')
% color stream figure
c.h = figure;
c.im = imshow(color,[]);
title('Color Source');
nFrame=0;
while true
% Get frames from Kinect and save them on underlying buffer
validData = k2.updateData;
% Before processing the data, we need to make sure that a valid
% frame was acquired.
if validData
% Copy data to Matlab matrices
depth = k2.getDepth;
color = k2.getColor;
% update depth figure
depth8u = uint8(depth*(255/outOfRange));
depth8uc3 = repmat(depth8u,[1 1 3]);
set(d.im,'CData',depth8uc3 + depthAdditions);
% update color figure
color = imresize(color,COL_SCALE);
% set(c.im,'CData',color + colorAdditions);
end
if ~isempty(nFrame)
if nFrame<=5
y=color();
red = y(:,:,1);
diffFrame = imsubtract(y(:,:,1), rgb2gray(y));
diffFrame = medfilt2(diffFrame, [3 3]);
binFrame = imbinarize(diffFrame, redThresh);
binFrame = bwareaopen(binFrame,300);
bw = bwlabel(binFrame, 8);
stats = regionprops((bw), 'BoundingBox', 'Centroid');
centroids = cat(1, stats.Centroid);
imshow(y);
hold on
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
X = bc(1); Y = bc(2);
Xpos = 1920-X; Ypos = Y;
Y_Rob = Ypos*Y_mm_to_pixel; %line 80 here variable 'Y_Rob' is defined
X_Rob = Xpos*X_mm_to_pixel;
disp('Input Color Coordinates')
disp([X Y])
depthCoords = k2.mapColorPoints2Depth([X/COL_SCALE Y/COL_SCALE]);
disp('Output depth coordinates')
disp(depthCoords)
depthAdditions = insertMarker(depthAdditions,depthCoords,'Color','green');
a=depthCoords(1,2);b=depthCoords(1,1);
Z=depth(a,b);
disp(Z)
Zk = 675; %distance between sensor and plane where objects are
%placed.
Zcoord = Zk - Z;
disp(Zcoord)
end
hold off
x=Y_Rob;
y=X_Rob; %Matlab says there is an error here becaause Y_Rob is not defined
z=Zcoord;
x=int2str(x);
y=int2str(y);
z=int2str(z);
s=strcat('[',x,',',y,',',z,']')
%Send a message to the robot
fwrite(tc, s);
pause(15)
end
end
nFrame = nFrame+1;
end
5 Comments
Walter Roberson
on 23 Jul 2019
Note that this is an issue for the very first frame. If the first frame is detected with content then the variables will be created and will still exist for the second frame even if no content is detected for it... The variables simply would not have content that was relevant to the frame in that case.
Moral of the story: initialize your variables if you are going to count on them existing.
Joel Handy
on 23 Jul 2019
Walter points out another bug waiting to happen. If any frame other the first has no detected content, the plot will simply replot the data for the previous frame and transmit incorrect info to the robot referenced in the comment.
Answers (1)
Stephen23
on 23 Jul 2019
Edited: Stephen23
on 23 Jul 2019
stats is empty so your loop never runs (i.e. the code inside the loop never runs so those variables are not defined). This occurs when no objects are detected in the image, something that your code does not take into account.
2 Comments
Stephen23
on 23 Jul 2019
" How exactly can I remove this error?"
You need to decide what your code should do when no objects are detected. If no objects are detected, should your code:
- throw an error?
- plot an empty graph?
- jump to the next while loop iteration?
- something else... ?
"So wat exactly am I supposed to change on the code so that it works perfectly"
I cannot answer this question, because you did not define what "perfectly" means: does "perfectly" mean that your code should recognise an invalid frame and throw an error, or that it should quietly move on to the next frame? I have no idea, because I am not your code's designer. You are. I do not know what algorithm you are implementing. You do. I do not know what your output specifications are. You do.
Once you decide what you want to do when no objects are detected in a frame, then you need to implement what you decided. For example, if you want to skip the rest of the code for that while loop iteration then you might find if and/or break to be useful.
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!