MATLAB: Image rendering in GUI

gui matlab image

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

Best Answer

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.