Help with patch in a for loop

15 views (last 30 days)
I'm having a problem with patch in a for loop where I want it to change the positions of each rectangle created from patch to mimic an object moving with velocity over a given timestep but its plotting every shapes instance instead and I'm not sure how to solve this. I've tried putting the function hold off in but it doesnt seem to work unless I've put it in the wrong place. However, I'm not sure it's suitable as I want to keep the previous plot of roadlines in. The bit of code that has the problem is at the bottom but I provided the rest of the code
nStarts=2;
nCarsPerStart=100;
time=100;
deltaT=1;
maxCarVelocity=10;
patchCarPosWithTimeStepX=NaN(4,nStarts*nCarsPerStart);
patchCarPosWithTimeStepY=NaN(4,nStarts*nCarsPerStart);
startX=[0; 4.5; 4.5; 0];
startY=[0.9 4.5; 0.9 4.5; 2.7 6.3; 2.7 6.3];
nStarts2=NaN(nStarts,(time/deltaT));
for n=1:1:nStarts
nStarts2(n,:)=(n:nStarts:nStarts*(time/deltaT));
end
carPosition=carCoords(nStarts,nCarsPerStart);
%--------------------------%
function [carPosition] = carCoords(nStarts,nCarsPerStart)
% Creates a matrix of car positions based on their starting locations
% Creates a NaN matrix and each location( or a lane in the model) has a
% set number of vehicles generated backwards from the start coord of 0.
% This is a cumulative sum from a random integer between two values to replicate the
% semi-random disribution of distance between cars on a road.
carPosition=NaN(nStarts*nCarsPerStart,nStarts);
for i=1:nStarts
carPosition((1:nCarsPerStart)+(i-1)*nCarsPerStart,i)=cumsum([0; randi([-14,-8],nCarsPerStart-1 ,1)]);
end
end
%-----------------------------------%
carProperties=intitalCarProperties(nStarts,nCarsPerStart,maxCarVelocity);
%------------------------------------%
function [carProperties] = intitalCarProperties(nStarts,nCarsPerStart,maxCarVelocity)
% Creates a matrix indicating each car's properties in the simulation.
% Detailed explanation goes here
% Max car velocity = 30 m/s ~ to 60mph in UK
driverBehaviour=1; % 1 = Normal behaviour
typeOfVehicle=1; %1 = Car (Average 2D dimensions = 4.7m x 1.9m)
%Starting location = number to indicate which location from the number of%
%starts. E.g. 1st location = 1 and 2nd location = 2%
carProperties=zeros(nStarts*nCarsPerStart,4);
for i=1:nStarts*nCarsPerStart
carProperties(:,1)=maxCarVelocity;
carProperties(:,2)=driverBehaviour;
carProperties(:,3)=typeOfVehicle;
end
end
%----------------------------%
carPosWithTimeStep=repmat(carPosition,1,time+1);
timeSteps = repmat(0:deltaT:time,nStarts,1);
timeSteps = timeSteps(1:end);
for i=1:1:nStarts*nCarsPerStart
for k=nStarts+1:1:nStarts*(time+1)
carPosWithTimeStep(i,k)=carPosWithTimeStep(i,k)+carProperties(i,1)*timeSteps(1,k);
end
end
roadLength=1000;
figure('units','normalized','position',[0 0 1 1])
road=plot ([0,roadLength],[7.3,7.3],'k',[0,roadLength],[3.65,3.65],'k--',...
[0,roadLength],[0,0],'k','linewidth', 1.5);
axis equal
for t=1:1:time
for q=1:1:nCarsPerStart*nStarts
for d=1:1:nStarts*(t/deltaT)
if carPosWithTimeStep(q,d)>=0
patchCarPosWithTimeStepX(:,q)=startX+carPosWithTimeStep(q,d);
else
continue
end
for o=1:1:nStarts
if find(any(d==nStarts2(o,:)))
patchCarPosWithTimeStepY(:,q)=startY(:,o);
else
continue
end
end
end
end
patch('XData',patchCarPosWithTimeStepX,'YData',patchCarPosWithTimeStepY,'FaceColor','Red');
drawnow
end

Accepted Answer

Walter Roberson
Walter Roberson on 25 Feb 2021
Record the handle of each patch object generated, and store it with the tracking position information for each object.
At any one time, decide whether a given object is a new object or is an existing object that moved.
If it is a new object, call your function to create the patch, and record the handle.
If it is not a new object, update XData and YData coordinates of the existing patch object.
If an object stops being tracked, either delete its associated patch, or else turn it invisible and add it to a cache of already-created patches ready to be dispatched next time a new object is needed.
  2 Comments
Jack Smillie
Jack Smillie on 25 Feb 2021
Edited: Jack Smillie on 25 Feb 2021
Is there any documentation on stuff related to that or an example code I can decipher the method from? I'm not really familar with how I'd go about doing what you said
Walter Roberson
Walter Roberson on 25 Feb 2021
if car_still_exists(q)
if isvalid(car_patches(q))
update_car_position(car_patches(q), patchCarPosWithTimeStepX(:,q), patchCarPosWithTimeStepY(:,q));
else
car_patches(q) = new_car_patch(patchCarPosWithTimeStepX(:,q), patchCarPosWithTimeStepY(:,q));
end
else
delete(car_patches(q))
end
function update_car_position(car_patch, X, Y)
set(car_patch, 'XData', X, 'YData', Y);
end
function car_patch = new_car_patch(X, Y)
car_patch = patch('XData', X, 'YData', Y, 'FaceColor', 'r')
end

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!