This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Log Data from an OPC Data Access Server

This example shows you how to configure and execute a logging session, and how to retrieve data from that logging session.


Create the OPC Toolbox Object Hierarchy

Create a hierarchy of OPC Toolbox™ objects.

da = opcda('localhost','Matrikon.OPC.Simulation.1');
grp = addgroup(da,'CallbackTest');

Configure the Logging Duration

Set the group's UpdateRate value to 0.2 seconds, and the RecordsToAcquire property to 40.

grp.UpdateRate = 0.2;
grp.RecordsToAcquire = 40;

Configure the Logging Destination

Configure the group to log data to disk and memory. Use a file in a temporary folder.

logFileName = fullfile(tempdir,'LoggingExample.olf');
grp.LoggingMode = 'disk&memory';
grp.LogFileName = logFileName;
grp.LogToDiskMode = 'overwrite';

The disk file name is LoggingExample.olf. If the file name exists, the toolbox engine overwrites the file.

Start the Logging Task

Start the logging task on the group object. Wait two seconds and show the last acquired value.

sPeek = peekdata(grp,1)
sPeek = 

    LocalEventTime: [2016 4 12 13 49 24.9080]
             Items: [3×1 struct]

Display the item ID and values

    'Random.Real8'    'Random.UInt2'    'Random.Real4'
    [  8.5714e+03]    [        9961]    [  1.9025e+04]

Wait for the object to complete logging before continuing with the example.


Retrieve the Data

Retrieve the first 20 acquired records into a structure.

sFirst = getdata(grp,20);

The getdata function removes the records from the OPC Toolbox engine. Examine the available records using the RecordsAvailable property of the group.

recAvail = grp.RecordsAvailable
recAvail =


Retrieve the balance of the records into separate arrays, converting all values to double-precision floating point numbers.

[exItmId,exVal,exQual,exTStamp,exEvtTime] = getdata(grp, ...

Examine the contents of the workspace.

whos ex*
  Name            Size            Bytes  Class     Attributes

  exEvtTime      20x1               160  double              
  exItmId         1x3               408  cell                
  exQual         20x3              8880  cell                
  exTStamp       20x3               480  double              
  exVal          20x3               480  double              

Retrieve data from disk for a specific item, using the 'itemids' filter.

sReal8Disk = opcread(logFileName,'itemids','Random.Real8')
sReal8Disk = 

40×1 struct array with fields:


Examine the second record.

ans = 

       ItemID: 'Random.Real8'
        Value: 1.4955e+04
      Quality: 'Good: Non-specific'
    TimeStamp: [2016 4 12 13 49 24.3890]

Clean Up

Disconnect and delete OPC Toolbox objects from the OPC Toolbox engine.


Deleting the client object also deletes the group and item objects.