%This M-file reads data from a WinDaq file and plots the data.

%ReadWinDaqFile takes 4 parameters:

%WinDaqFileName = full path to the WinDaq (.wdq) data file.

%StartScan = scan point to start collecting data from, 0 = beginning of
%file.

%NumScanPts = number of channel scans to be returned. One scan is defined
%as one pass through all enabled channels (i.e., one sample per channel). 
%This value must be between 1 and 32,767.  NOTE: the scan starts at 0, so
%if you want 200 scans, enter NumScanPts = 199

%DataFormat = format of the data returned. Valid settings are: 0 for 2’s
%complement binary data (FormatBinary); and 1 for data scaled in 
%engineering units (FormatScaled).

function dqAnalysis = ReadWinDaqFile(WinDaqFileName,StartScan,NumScanPts,DataFormat) 

%Create the readdataqfile object in a figure window
readdataqfile1 = actxcontrol('DATAQFILE.ReadDataqFileCtrl.1',[10,10,5,5]);

%Register the ReadDataqFile Events
readdataqfile1.registerevent({'FileError','dataqfileerror';'EndofFile','endoffile';'ControlError','controlerror'});

%Select the WinDaq file that you want to read from
set(readdataqfile1, 'FileName', WinDaqFileName);

%Open the WinDaq file
readdataqfile1.Open;

%Move the pointer to the StartScan position from the beginning of the
%file(0)
readdataqfile1.MoveTo(StartScan, 0);

%Display data on the screen, and put into matrix dqAnalysis
dqAnalysis = readdataqfile1.GetData(NumScanPts, DataFormat);

%View the number of Channels and data rows in the file and assign them to
%variables  dqRows, dqChannels
[dqChannels,dqDataPts] = size(dqAnalysis);

%Create a time vector, dqTime, of integers from 1 to dqDataPts. 
EndScan = dqDataPts + StartScan - 1;
dqTime = StartScan:EndScan;

%Plot the channels versus data points and annotate the plot
set(0,'defaultaxeslinestyleorder','-')
set(0,'defaultaxescolororder',[1 0 0;0 1 0;0 0 1;1 1 0;0 0 0;1 0 1;0 1 1])
plot(dqTime,dqAnalysis)
xlabel('WinDaq Data Scans'), ylabel('Units Measured'), grid on

%This function closes ReadDataqFile and deletes it.  Delete the next two
%lines if you want to use readdataqfile1 after you run the function.
%NOTE: the output variable (value = dqAnalysis data matrix) will still be 
%useable for computations.
readdataqfile1.Close
readdataqfile1.delete

%For more information about using the DATAQ ActiveX controls in MATLAB, go
%to http://www.dataq.com/develop/
%For help with this file, please post a message to the DATAQ Forum at
%http://support.dataq.com
