How to write a video file with analog plots?

2 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2019-5-1
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 2019-5-4
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?

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2019-5-4
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 个评论
horizon
horizon 2019-5-6
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by