"Error using matlab.graphics.chart.primitive. Line/set."
Show older comments
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
on 21 Feb 2017
Edited: Walter Roberson
on 21 Feb 2017
I copied and pasted your code exactly and had no difficulty with the error you mention. It took a while for it to get out of the center zone but moved quickly once it did.
S Foggie
on 21 Feb 2017
Walter Roberson
on 22 Feb 2017
Your posted code has the EraseMode bit commented out, so it would not give a warning ;-)
S Foggie
on 22 Feb 2017
Accepted Answer
More Answers (1)
Ataklti Kahsay
on 17 Jun 2021
0 votes
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, ...
1 Comment
Walter Roberson
on 17 Jun 2021
You asked this over at https://www.mathworks.com/matlabcentral/answers/268040-how-to-eliminate-or-exclude-complix-numbers-from-an-array-solution#comment_1589880 and I responded there.
(You should have created your own Question, really.)
Categories
Find more on Entering Commands 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!