Hello all. I have a problem with rendering my image in GUI. I'm trying to creat a automatic simulation of crossroad with traffic lights. First of all I need to move with cars, which I load as a image to GUI. The problem is that when the for loop starts rendering image of the car(Image number 1), it does not delete the former images which is unacceptable (image number 2). Can anyone help me with this, please?
clc
clear
I=imread('C:\Users\Miroslav\Desktop\Crossroad\cross_matl.png');
hi = imagesc(I)
hold on
car=imread('C:\Users\Miroslav\Desktop\Crossroad\blue_car3.png');
Nt=10; % Number of time steps
xval=450;
ymin=900;
ymax=-75;
y=linspace(ymin,ymax,Nt);
for it=1:Nt
imagesc('CData',car,'XData',[450],'YData',[y(it)])
pause(1);
end

 Risposta accettata

Geoff Hayes
Geoff Hayes il 19 Mar 2020
Miroslav - rather than creating a new image (position) for the car on each iteration of the loop, try updating the YData (vertical position) for the car. For example,
Nt=10; % Number of time steps
xval=450;
ymin=900;
ymax=-75;
y=linspace(ymin,ymax,Nt);
hCar = imagesc('CData',car,'XData',[450],'YData',[y(1)])
for it=1:Nt
set(hCar,'YData',[y(it)]);
pause(1);
end
I haven't tried the above but I think it could work.

8 Commenti

Miroslav Jiránek
Miroslav Jiránek il 19 Mar 2020
It works, thank you so much.
Now I would like to extend this program for up to 16 cars(4 cars for each road) but 16 for cycles for each car is probably useless, I guess. Do you have any idea how to program this?
Geoff Hayes
Geoff Hayes il 19 Mar 2020
Miroslav - you could create an array of 16 cars, an array (matrix) of 16 x-coordinates (for each iteration), and an array (matrix) of 16 y-ccordinates (for each iteration. So you would have a 16x1 array of handles (for the different cars) and two 16xNt arrays for the x- and y-coordinates. There would just be one for loop for the Nt iterations and an inner loop to iterate over each car.
Miroslav Jiránek
Miroslav Jiránek il 19 Mar 2020
but in this case, the next car will wait until the previous car runs from start to finish, right? I need to get up to 16 cars together on the road at once
Well if the outer for loop handles the iterations, and the inner for loop handles the cars, then each car would move once per iteration:
numCars = 16;
for it=1:Nt
for k = 1:numCars
set(hCars(k),'XData', x(k,it), 'YData', y(k,it));
end
pause(1);
end
In the above, I'm assuming that hCars is an array of handles to each of the cars, and that x and y are arrays of dimension 16xNt.
Miroslav Jiránek
Miroslav Jiránek il 19 Mar 2020
Modificato: Miroslav Jiránek il 19 Mar 2020
I'm afraid it doesn't work. It wrotes that the line above exceeds matrix dimensions.
'set(hCars(k),'XData', x(k,it), 'YData', y(k,it));'
Geoff Hayes
Geoff Hayes il 19 Mar 2020
How have you defined hCars, x, and y? What are their dimensions? You will need to change your above code to account for the x- and y-coordinates for each car for each of the sixteen iterations. Please show more of your code.
backgeound=imread('C:\Users\Miroslav\Desktop\Crossroad\cross_matl.png');
hi = imagesc(backgeound)
hold on
car=imread('C:\Users\Miroslav\Desktop\Crossroad\blue_car3.png');
Nt=500; % Number of time steps
xval=450;
ymin=900;
ymax=-75;
y=linspace(ymin,ymax,Nt);
hCars = imagesc('CData',car,'XData',[450],'YData',[y(1)]) %start location
numCars = 16;
for it=1:Nt
for k = 1:numCars
set(hCars(k), 'YData', y(k,it)); %car movement
end
pause(1);
end
But y is a 1x16 (or 16x1) array and hCars is a scalar (1x1)
y=linspace(ymin,ymax,Nt);
hCars = imagesc('CData',car,'XData',[450],'YData',[y(1)]) %start location
so the error message makes sense. You will need to determine the x and y for each of the sixteen cars (presumably half the cars are going east-west (or west-east) and so the y is fixed but the x is variable) and you will need to create a handle to each car image for each of the sixteen cars. If we just concentrate on the cars going north-south (or south-north), at iteration 1, what is the y position for each of the cars? What should the y position/coordinate be on the second iteration? Do the cars follow the other? Do they go the same speed?

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by