MATLAB Answers

Exporting data while 'live' processing in a GUI

13 views (last 30 days)
Sid
Sid on 25 Jun 2015
Answered: Sid on 7 Jul 2015
Hi everyone,
The question pertains to the export of data from a GUI that is 'live' processing data from an incoming datastream. The initial question came from some work by Jerome, and is located here on his website ( Link.)
(In order to save time, I have the code from Jerome posted below as well. Please note that this script requires Image Acquisition Toolbox.)
function realVideo()
% Define frame rate
NumberFrameDisplayPerSecond=10;
% Open figure
hFigure=figure(1);
% Set-up webcam video input
try
% For windows
vid = videoinput('winvideo', 1);
catch
try
% For macs.
vid = videoinput('macvideo', 1);
catch
errordlg('No webcam available');
end
end
% Set parameters for video
% Acquire only one frame each time
set(vid,'FramesPerTrigger',1);
% Go on forever until stopped
set(vid,'TriggerRepeat',Inf);
% Get a grayscale image
set(vid,'ReturnedColorSpace','grayscale');
triggerconfig(vid, 'Manual');
% set up timer object
TimerData=timer('TimerFcn', {@FrameRateDisplay,vid},'Period',1/NumberFrameDisplayPerSecond,'ExecutionMode','fixedRate','BusyMode','drop');
% Start video and timer object
start(vid);
start(TimerData);
% We go on until the figure is closed
uiwait(hFigure);
% Clean up everything
stop(TimerData);
delete(TimerData);
stop(vid);
delete(vid);
% clear persistent variables
clear functions;
% This function is called by the timer to display one frame of the figure
function FrameRateDisplay(obj, event,vid)
persistent IM;
persistent handlesRaw;
persistent handlesPlot;
trigger(vid);
IM=getdata(vid,1,'uint8');
if isempty(handlesRaw)
% if first execution, we create the figure objects
subplot(2,1,1);
handlesRaw=imagesc(IM);
title('CurrentImage');
% Plot first value
Values=mean(IM(:));
subplot(2,1,2);
handlesPlot=plot(Values);
title('Average of Frame');
xlabel('Frame number');
ylabel('Average value (au)');
else
% We only update what is needed
set(handlesRaw,'CData',IM);
Value=mean(IM(:));
OldValues=get(handlesPlot,'YData');
set(handlesPlot,'YData',[OldValues Value]);
end
Imagine a case where you are tracking data from a webcam for an 8 hour session. In order to prevent the risk of losing all data in one save session at the end of the day, I would like to break the session into, say, 30 min sessions that I want to save, while still maintaining a display window active.
My initial thought was to save some maintain some type of a counter, where upon reaching 30 minutes, it would output a file (i.e. 'YData', [OldValues Value].) However, I am not sure if the write time can potentially have an impact on the data analysis/updating screen. Is this something that can be introduced in as a separate timerFcn?
I'm guessing that it's not the first time this question has been asked, so any direction/thoughts would be most appreciated.
Thanks.

Accepted Answer

Adam Hug
Adam Hug on 30 Jun 2015
Before investing a lot of effort into coding this up, try running a simple experiment where you save one frame every 1/60 of a second (or whatever your video camera's sampling rate is) and then output it to your screen. If this cannot be done in real time, then you will need to inevitably drop frames or reduce the image quality in your final save. This kind of setup is very hardware dependent so it is a good idea to know your limitations going in.

More Answers (1)

Sid
Sid on 7 Jul 2015
Adam, thanks for the thought, and yes, something I am learning as I am going. For the purposes of this particular application, I set the timer to drop in the BusyMode , because I could afford to have some level of error across a long span of time (i.e. I'm ok to lose a few frames here and there across an 8 hour period.)
To close the loop on the original question on periodically saving data out, I ended up setting a persistent variable named counter that is set up to increment by one every time a frame is grabbed in the TimerFcn . Then, when the counter reaches the desired limit, simply write the YData out to a file, clear out the counter and start all over again.
I do lose some data during the write process, but I figured this is a level of error I will have to live with, without going to a different hardware configuration.

Community Treasure Hunt

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

Start Hunting!