%% Logging Data To Disk
%
% Copyright 2001-2014 The MathWorks, Inc.
%
% This example shows how to configure logging properties for disk logging 
% and then initiate an acquisition to log.
%
%% Configuring Logging Mode
% Data acquired from an image acquisition device may be logged to memory,
% to disk, or both. By default, data is logged to memory. To change the
% logging mode, configure the video input object's |LoggingMode| property.

% Access an image acquisition device, using a grayscale video format with
% 10 bits per pixel.
vidobj = videoinput('gige', 1, 'Mono10');

% View the default logging mode.
currentLoggingMode = vidobj.LoggingMode;

%%

% List all possible logging modes.
set(vidobj, 'LoggingMode')

%%

% Configure the logging mode to disk.
vidobj.LoggingMode = 'disk';

% Verify the configuration.
currentLoggingMode = vidobj.LoggingMode;

%% Configuring Disk Logging Properties
% Logging to disk requires a MATLAB(R) |VideoWriter| object. |VideoWriter|
% is a MATLAB function, not a toolbox function. After you create and
% configure a |VideoWriter| object, provide it to the video input object's
% |DiskLogger| property.
%
% |VideoWriter| provides a number of different profiles that log the
% data in different formats.  This example uses the Motion JPEG 2000 profile
% which can log single-banded (grayscale) data as well as
% multi-byte data.  The complete list of profiles provided by |VideoWriter|
% can be found in the documentation.

% Create a |VideoWriter| object.
logfile = VideoWriter('logfile.mj2', 'Motion JPEG 2000')

%%

% Configure the video input object to use the |VideoWriter| object.
vidobj.DiskLogger = logfile;

%% Initiating the Acquisition
% Now that the video input object is configured for logging data to 
% a Motion JPEG 2000 file, initiate the acquisition.

% Start the acquisition.
start(vidobj)

% Wait for the acquisition to finish.
wait(vidobj, 5)

%%
% When logging large amounts of data to disk, disk
% writing sometimes lags behind the acquisition. To determine whether all
% frames have been written to disk, use the |DiskLoggerFrameCount|
% property.

while (vidobj.FramesAcquired ~= vidobj.DiskLoggerFrameCount)
    pause(.1)
end

%%
% Verify that the |FramesAcquired| property and the |DiskLoggerFrameCount|
% property have the same value.
vidobj.FramesAcquired

%%
vidobj.DiskLoggerFrameCount

%%

% When the video input object is no longer needed, delete
% it and clear it from the workspace.
delete(vidobj)
clear vidobj
