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);endclose(writerObj);
My Code
writerObj = VideoWriter('graph.avi');open(writerObj);tx = daq.createSession('ni');s = daq.createSession('ni');s.Rate = 400000;ultraFreq = 40000;numCycle =8y = [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)endfunction queueMoreData(src, event) queueOutputData(tx, y');end
Best Answer