How to write a video file with analog plots?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
horizon
el 1 de Mayo de 2019
Comentada: horizon
el 6 de Mayo de 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 comentarios
Walter Roberson
el 1 de Mayo de 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.
Respuesta aceptada
Walter Roberson
el 4 de Mayo de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!