"Error using matlab.gra​phics.char​t.primitiv​e. Line/set."

236 views (last 30 days)
I'm trying to write the langston's ant program. However, I keep getting the "Error using matlab.graphics.chart.primitive.Line/set.Invalid or deleted object."
My square cursor steps as it should, but I'm not getting the line marker to show the ant's progress. Here's the part of my code that I'm having trouble with. Any help would be appreciated.
Thanks
clear all
close all
% initialize grid to all zeros (i.e., white)
gridcolor = zeros(100,100);
% specify the bounds of the grid
x_min = 1; x_max = 100;
y_min = 1; y_max = 100;
% initialize ant position to the middle of the grid
ant_x = 50;
ant_y = 50;
% initialize the ant direction to east
antdirection = 0;
% create initial plot with handle 'p', specifying the x and y values as
% ant_x and ant_y
p=plot(ant_x,ant_y,'square');%, 'EraseMode','none');
% Set plot axis to span the grid
axis([0 100 0 100])
inside = 1;
% The number of steps needed to reach the edge of the grid
total_steps = 0;
while inside == 1,
total_steps = total_steps + 1;
if gridcolor(ant_x,ant_y) == 0, % if the square is white, then
antdirection = antdirection + 90; % turn 90 deg to the right
else % otherwise the square is black, so
antdirection = antdirection - 90; % turn 90 deg to the left
end
% check antdirection and modify if necessary to keep it in the range of
% 0 to 360 degrees
if antdirection >= 360,
antdirection = antdirection - 360;
elseif antdirection < 0,
antdirection = antdirection + 360;
end
% the ant always flips the color of the square that it is on
gridcolor(ant_x,ant_y) = ~gridcolor(ant_x,ant_y);
if gridcolor(ant_x,ant_y) == 0, % if the grid square is white, then
% set the Marker edge and face color to white for the x,y point
set(p,'MarkerEdgeColor','w','MarkerFaceColor','w','XData',ant_x,'YData',ant_y)
else % the grid square is black, so
%set the Marker edge and face color to black for the x,y point
set(p,'MarkerEdgeColor','black','MarkerFaceColor','black','XData',ant_x,'YData',ant_y)
end
drawnow
% determine the x and y direction of the ant motion
ant_motion_x = cosd(antdirection);
ant_motion_y = sind(antdirection);
% move the ant to the next square
ant_x = ant_x + ant_motion_x;
ant_y = ant_y + ant_motion_y;
% determine if the ant has move outside the grid; if so, set inside to
% 0 so that the loop terminates
if (ant_x<x_min) || (ant_x>x_max),
inside = 0;
elseif (ant_y<y_min) || (ant_y>y_max),
inside = 0;
end
end
  4 Comments
Walter Roberson
Walter Roberson on 22 Feb 2017
Your posted code has the EraseMode bit commented out, so it would not give a warning ;-)
S Foggie
S Foggie on 22 Feb 2017
Yes, sorry. I was debugging and inadvertently copied my version with the line commented out to let it run without errors. The warning says ErasedMode is no longer supported after ver.2014a and should be replaced with Animatedline but I haven't figured out how to make it work.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 22 Feb 2017
Adjusted code. This does away with drawmode entirely and works on images.
You might want to add
set(ax, 'YDir', 'reverse')
  4 Comments
Jonathan
Jonathan on 22 Jul 2019
@Mr Roberson
I would to ask something in line with this. I am using a Kin2 Toolbox for Matlab to map depth frames from color with Kinect V2 sensor. I am getting this error:
"Error using matlab.graphics.primitive.Image/set Invalid or deleted object"
Can you help me on figuring out what might be the problem. The code is as follows
clear; clc;
addpath('Mex');
k2 = Kin2('color','depth');
X_mm_to_pixel = 1190/1920; Y_mm_to_pixel = 670/1080;
redThresh = 0.25;
% 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<=100
s=color();
red = s(:,:,1);
diffFrame = imsubtract(s(:,:,1), rgb2gray(s));
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(s);
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);
X_pos = 1920-X; Y_pos = Y;
X_Rob = X_pos*X_mm_to_pixel;
Y_Rob = Y*Y_mm_to_pixel;
y = X_Rob; x = Y_Rob;
ObjPos = [x y];
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)
end
hold off
x=Y_Rob;
y=X_Rob;
z=Z
x=int2str(x);
y=int2str(y);
z=int2str(z);
s=strcat('[',x,',',y,',',z,']')
pause(3)
end
nFrame = nFrame+1;
end
end
Walter Roberson
Walter Roberson on 30 Jul 2019
The typical cause of that error in the kind of code structure you are using, is that the problem occurs upon closing the figure without having stopped the update loop. That leaves the loop trying to update a deleted object.
After the pause() call, check isvalid() on the figure and exit the loop if not.

Sign in to comment.

More Answers (1)

Ataklti Kahsay
Ataklti Kahsay on 17 Jun 2021
please help me i have faced thesame problem
Obj =
1.0e+13 *
2.3579 + 0.0097i 0.0106 + 0.0000i
Obj =
1.0e+11 *
4.0416 + 2.4312i 0.0186 + 0.0110i
Obj =
1.0e+12 *
1.8716 + 0.2003i 0.0085 + 0.0009i
Obj =
1.0e+11 *
8.6100 + 3.2635i 0.0392 + 0.0147i
Obj =
1.0e+11 *
3.5387 + 3.1425i 0.0164 + 0.0142i
Warning: One or more feasible individuals has a complex fitness function value. gamultiobj is using the real part of the
fitness function values
> In rankAndDistance (line 21)
In gamultiobjMakeState (line 199)
In gamultiobjsolve (line 20)
In gamultiobj (line 304)
In Main_Genetic_Algorithm (line 41)
Warning: Imaginary parts of complex X and/or Y arguments ignored.
> In gaplotpareto>plotFirstFront (line 72)
In gaplotpareto (line 28)
In gadsplot>callOnePlotFcn (line 194)
In gadsplot (line 141)
In gamultiobjsolve (line 24)
In gamultiobj (line 304)
In Main_Genetic_Algorithm (line 41)
Warning: Using only the real component of complex data.
> In matlab.graphics.chart.internal.getRealData (line 52)
In bar (line 139)
In gaplotscores (line 41)
In gadsplot>callOnePlotFcn (line 194)
In gadsplot (line 141)
In gamultiobjsolve (line 24)
In gamultiobj (line 304)
In Main_Genetic_Algorithm (line 41)
Obj =
1.0e+12 *
1.1881 + 0.2512i 0.0054 + 0.0011i
Obj =
1.0e+11 *
3.1144 + 3.0470i 0.0145 + 0.0137i
Obj =
1.0e+11 *
3.7438 + 2.8514i 0.0173 + 0.0128i
Obj =
1.0e+11 *
3.0983 + 3.0006i 0.0144 + 0.0135i
Obj =
1.0e+11 *
4.5208 + 1.6571i 0.0207 + 0.0075i
Obj =
1.0e+11 *
3.0157 + 2.9600i 0.0140 + 0.0133i
Error using matlab.graphics.chart.primitive.Line/set
Complex values are not supported.
Error in gaplotpareto>plotFirstFront (line 84)
set(plotHandle,'Xdata',xy(:,1), 'Ydata',xy(:,2));
Error in gaplotpareto (line 28)
plotFirstFront(state.Score(range,:),state.Rank(range),markers{1 + mod(i,5)},objectivesToPlot(:)',flag,tag);
Error in gadsplot>callOnePlotFcn (line 194)
optimvalues = plotfcn(varargin{1:end});
Error in gadsplot (line 148)
[state,optimvalues] =
callOnePlotFcn(fname,plotNames{i},state,options.OutputPlotFcnOptions,optimvalues,flag,args{i}{:});
Error in gamultiobjsolve (line 64)
state = gadsplot(options,state,currentState,'Genetic Algorithm');
Error in gamultiobj (line 304)
[x,fval,exitFlag,output,population,scores] = gamultiobjsolve(FitnessFcn,nvars, ...
Error in Main_Genetic_Algorithm (line 41)
[X, fval, exitflag, output, population, scores] = gamultiobj(@Fitness_Function, ...

Community Treasure Hunt

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

Start Hunting!