Help with patch in a for loop
Show older comments
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
More Answers (0)
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!