Main Content

Retrieve Large Historical Data Sets

This example shows how to retrieve very large data sets from OPC historical data access servers.

Your OPC HDA server may have a defined upper limit on how much data to return in any given historical data access read operation. That upper limit is returned by the MaxReturnValues field of the structure returned by calling getServerStatus on the client object. A value of 0 means there is no defined limit, and the server returns all possible values.

When you request data over a wide time range, the server returns up to MaxReturnValues elements for each item, and the read function issues a warning. The warning ID is opc:hda:mex:ReadMoreData. To retrieve all values, use code similar to that shown here.

This example retrieves all values of two items over a full year.

lastwarn('');
startTime = datetime(2013,1,1);  % Replace with your start time
endTIme = datetime(2013,12,31);  % Replace with your end time
itmList = {'Plant1.Unit2.FIC1001', 'Plant2.Unit1.FIC1001'}; % Replace with your item list
wState = warning('off','opc:hda:mex:ReadMoreData');
yearData = hdaObj.readRaw(itmList,startTime,endTime);
[warnMsg, warnID] = lastwarn;
gotAllData = isempty(strfind(warnID,':ReadMoreData')) && isempty(strfind(warnID,':ReadComposite'));
while ~gotAllData
    % Update start time to last time retrieved
    endDates = cellfun(@(x)x(end), {yearData.TimeStamp});
    startTime = max(endDates);
    % Read data and append to existing data set
    moreData = hdaObj.readRaw(itmList,startTime,endTime);
    yearData = append(yearData,moreData);
    [warnMsg, warnID] = lastwarn;
    gotAllData = isempty(strfind(warnID,':ReadMoreData'));
end
% Reset warning state
warning(wState);