How to write a video file with analog plots?

2 vues (au cours des 30 derniers jours)
horizon
horizon le 1 Mai 2019
Commenté : horizon le 6 Mai 2019
I would like to write a graph to a video file.
It was succeeded to make a video to recode a graph with the following simple code, but I have no idea how this program is applicable to my program.
Though I tried to edit my code to recode plotting data for ten seconds of video, the file size of the video was 0 KB.
How should I fix my code?
Simple Code to create a video file from a graph
x = 0:100;
y = sin(x);
writerObj = VideoWriter('test.avi');
open(writerObj);
fig = plot(x,y);
set(fig, 'XDataSource', 'x');
set(fig, 'YDataSource', 'y');
set(gca, 'Xlim', [0 100], 'Ylim', [-20 20]);
set(gca,'nextplot','replacechildren');
for k = 0:20
y = k*sin(x);
refreshdata(fig);
frame = getframe(gcf);
writeVideo(writerObj, frame);
end
close(writerObj);
My Code
writerObj = VideoWriter('graph.avi');
open(writerObj);
tx = daq.createSession('ni');
s = daq.createSession('ni');
s.Rate = 400000;
ultraFreq = 40000;
numCycle =8
y = [y zeros(1,numCycle*s.Rate/ultraFreq)];
addAnalogOutputChannel(tx, 'Dev1', 'ao0', 'Voltage');
th=addlistener(tx, 'DataRequired', @queueMoreData);
addAnalogInputChannel(s,'Dev1', 'ai0', 'Voltage');
ch = addAnalogInputChannel(s, 'Dev1', 'ai1', 'Voltage');
h = addlistener(s, 'DataAvailable', @plotData);
s.DurationInSeconds(1);
queueOutputData(tx, y');
startBackground(s);
tx.startForeground();
function plotData(src, event)
t1 = event.TimeStamps(:,1);
s1 = event.Data(:,1);
s2 = event.Data(:,2);
subplot(2,1,1)
plot(t1,s1)
ylim([-10.0 10.0]);
title('s_1')
subplot(2,1,2)
plot(t1,s2)
ylim([-10.0 10.0]);
title('s_2')
xlabel('Time (s)')
while(10)
frame = getframe(gcf);
writeVideo(writerObj, frame)
end
function queueMoreData(src, event)
queueOutputData(tx, y');
end
  2 commentaires
Walter Roberson
Walter Roberson le 1 Mai 2019
while(10) is an infinite loop. You would have to abort to get out of it, and that would leave the video unfinished.
writerObj is not within scope of function plotData. Easiest would probably be to use nested functions.
horizon
horizon le 4 Mai 2019
Thank you for your comment.
Why writerObj is not a global variable in this case?
It is defined before calling the function and how can I pass it to the function? Using a .mat file or redefine writerObj inside of the function?

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 4 Mai 2019
function plot_session
recording_time = 300; %seconds
ax1 = subplot(2,1,1);
L1 = animatedline(ax1);
ylim(ax1, [-10.0 10.0]);
title(ax1, 's_1')
xlabel(ax1, 'Time (s)')
ax2 = subplot(2,1,2);
L2 = animatedline(ax2);
ylim(ax2, [-10 10.0]);
title(ax2, 's_2')
xlabel(ax2, 'Time (s)')
writerObj = VideoWriter('graph.avi');
open(writerObj);
tx = daq.createSession('ni');
s = daq.createSession('ni');
s.Rate = 400000;
ultraFreq = 40000;
numCycle = 8;
y = [y zeros(1,numCycle*s.Rate/ultraFreq)];
addAnalogOutputChannel(tx, 'Dev1', 'ao0', 'Voltage');
th = addlistener(tx, 'DataRequired', @queueMoreData);
addAnalogInputChannel(s,'Dev1', 'ai0', 'Voltage');
ch = addAnalogInputChannel(s, 'Dev1', 'ai1', 'Voltage');
h = addlistener(s, 'DataAvailable', @plotData);
s.DurationInSeconds(1);
queueOutputData(tx, y');
startBackground(s);
tx.startForeground();
pause(recording_time)
%cleanup time
delete(th)
delete(h)
pause(1); %time for last plot event
close(writerObj);
delete(s)
delete(tx)
function plotData(src, event)
t1 = event.TimeStamps(:,1);
s1 = event.Data(:,1);
s2 = event.Data(:,2);
addpoints(L1, t1, s1);
addpoints(L2, t1, s2);
drawnow();
pause(0.05); %in practice need time to render
frame = getframe(gcf);
writeVideo(writerObj, frame)
end
function queueMoreData(src, event)
queueOutputData(tx, y');
end
end
  1 commentaire
horizon
horizon le 6 Mai 2019
Thank you for your answer.
Does this answer is related to the following another question?
I have trouble with understanding how to use and the applicable cases of pause function on MATLAB.
I would appreciate if you could help me again.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by