MATLAB Answers

Image rendering in GUI

1 view (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 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 Comments

Show 5 older comments
Geoff Hayes
Geoff Hayes on 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.
Miroslav Jiránek
Miroslav Jiránek on 19 Mar 2020
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
Geoff Hayes
Geoff Hayes on 19 Mar 2020
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?

Sign in to comment.

More Answers (0)