Main Content

Update MATLAB Plots While Logging OPC Data

This example shows you how to use a custom callback to plot data acquired during an OPC logging task.

The example makes use of the display_opcdata function, which plots recently acquired data in a figure window.


Step 1: Create the OPC Object Hierarchy

Create a hierarchy of OPC objects.

da = opcda('localhost','Matrikon.OPC.Simulation.1');
grp = addgroup(da,'CallbackTest');
additem(grp,'Triangle Waves.Real8');
additem(grp,'Saw-toothed Waves.UInt2');

Step 2: Configure Property Values

Configure the logging task to acquire 200 records at 0.1 second intervals.

grp.RecordsToAcquire = 200;
grp.UpdateRate = 0.1;

Specify the display_opcdata function as the RecordsAcquiredFcn callback, which must be called after each 10 records are acquired.

grp.RecordsAcquiredFcnCount = 10;
grp.RecordsAcquiredFcn = @display_opcdata;

Step 3: Acquire Data

Start the group object. After every 10 records are acquired, the object executes the display_opcdata callback function. This callback function plots the most recently acquired records logged to the memory buffer.


Step 4: Clean Up

Always remove OPC objects from memory when you no longer need them.


Deleting the client object disconnects the client from the server, and deletes the group and item objects.