Moving an image synchronized with a plot

6 views (last 30 days)
Lorenzo Calzolai
Lorenzo Calzolai on 11 Feb 2019
This is a problem I've been struggling with for some time now, I've found many examples of image manipulation in Matlab, but I couldn't wrap my head around it.
In short: I'm trying to move an image on the screen synchronized with a point on a plot. The point is the absolute minimum of the plot during the time of the animation and the image is a little car in png format. The code below is what I wrote till now, it creates the video with the plot minus the little car.
Can someone help me with this? Thanks.
clear all
ciclo=6;
if(ciclo == 5)
samples=40;
else
samples=60;
end
measures=[6 8 11 13 16 18 21 23];
assex=[6.538 8.925 11.31 13.7 16.18 18.66 21.21 23.54];
[~,N]=size(measures); %number of measures
spost_m=zeros(samples,N);
for hh=1:N
m=measures(hh);
load(sprintf('Misure\\spost%d_%dm_correct',ciclo,m))
spost_m(:,hh)=spost1(:);
end
%interpolazione spost_m
i_f=4; %interpolation factor
spost_2m=zeros(samples,i_f*N);
spost_3m=zeros(i_f*samples,i_f*N);
for hh=1:samples
xo1 = 1:numel(spost_m(hh,:));
xi1 = linspace(1,numel(spost_m(hh,:)),i_f*N);
spost_2m(hh,:)=interp1(xo1,spost_m(hh,:),xi1);
end
for hh=1:i_f*N
xo1 = 1:numel(spost_2m(:,hh));
xi1 = linspace(1,numel(spost_2m(:,hh)),i_f*samples);
spost_3m(:,hh)=interp1(xo1,spost_2m(:,hh),xi1);
end
%interpolazione assex
xo1 = 1:numel(assex);
xi1 = linspace(1,numel(assex),i_f*N);
assex2=interp1(xo1,assex,xi1);
A = imread('car_small.png');
figure
p=plot(assex2,spost_3m(1,:),'*-','LineWidth',1.5)
xlabel('Posizione [m]')
ylabel('Spostamento [mm]')
axis([-inf inf -10 10])
F(samples)=struct('cdata',[],'colormap',[]);
set(gcf,'Units','normalized','OuterPosition',[0 0 1 1]);
F(1)=getframe(gcf);
for ii=2:i_f*samples
p.XData=assex2;
p.YData=spost_3m(ii,:);
drawnow
F(ii)=getframe(gcf);
pause(0.01);
end
video=VideoWriter('video_test','MPEG-4');
video.FrameRate=10;
open(video);
writeVideo(video,F);
close(video);

Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by