How can sync a video to the plot?

4 views (last 30 days)
Sujan Ponnappa
Sujan Ponnappa on 9 Jan 2020
I am trying to sync the video to the plot such that when the video plays(slicing a potato on a pressure sensor) the corresponding pressure signals are plotted.
But I am not able to sync the speed of the video to the speed of the plot.
I have also attached the video and the excel file of the pressure data to this .
Could anyone help me to solve this?
rng default
Fs = 24;
xs = potatoslicetimesync.Force;
ts = (0:length(xs)-1/Fs);
%plot(ts,xs)
%% Setup the subplots
ax1 = subplot(2,1,1); % For video
ax2 = subplot(2,1,2); % For pressure plot
%%% Setup VideoReader object
filename = 'slicesync.avi';
v = VideoReader(filename);
frameratevideo=v.FrameRate;
s = VideoWriter('C:\Users\Suj\Desktop\Master Thesis\Videos\slicesync.avi');
s.FrameRate = 500;
nFrames = v.Duration*v.FrameRate; % Number of frames
% Display the first frame in the top subplot
vidFrame = readFrame(v);
v.CurrentTime = 0;
image(vidFrame, 'Parent', ax1);
ax1.Visible = 'off';
%%% Load the pressure data
t = ts; % Cooked up for this example, use your actual data
y = xs;
nDataPoints = length(t); % Number of data points
step = round((nFrames/nDataPoints));
index = 1:0.55:nDataPoints;
i = 2;
% Diplay the plot corresponds to the first frame in the bottom subplot
h = plot(ax2,t(1:index(i)),y(1:index(i)),'-k');
% Fix the axes
ax2.XLim = [t(1) t(end)];
ax2.YLim = [min(y) max(y)];
%%% Animate
while hasFrame(v)
pause(1/v.FrameRate);
%pause(0.01)
vidFrame = readFrame(v);
image(vidFrame, 'Parent', ax1);
ax1.Visible = 'off';
i = i + 1;
set(h,'YData',y(1:index(i)), 'XData', t(1:index(i)))
end
%%% Load the pressure data
t = ts; % your actual data
y = xs;
nDataPoints = length(t); % Number of data points
step = round((nFrames/nDataPoints));
index = 1:step:nDataPoints;
i = 2;
% Diplay the plot corresponds to the first frame in the bottom subplot
h = plot(ax2,t(1:index(i)),y(1:index(i)),'-k');
% Fix the axes
ax2.XLim = [t(1) t(end)];
ax2.YLim = [min(y) max(y)];
%%% Animate
while hasFrame(v)
pause(1/s.FrameRate);
%pause(0.01)
vidFrame = readFrame(v);
image(vidFrame, 'Parent', ax1);
ax1.Visible = 'off';
i = i + 1;
set(h,'YData',y(1:index(i)), 'XData', t(1:index(i)))
end

Answers (0)

Community Treasure Hunt

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

Start Hunting!