How to write a video file with analog plots?

3 visualizzazioni (ultimi 30 giorni)
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 Commenti
Walter Roberson
Walter Roberson il 1 Mag 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 il 4 Mag 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?

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 4 Mag 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 Commento
horizon
horizon il 6 Mag 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Prodotti


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by