Main Content

Manage OPC Data Access Objects

This example shows you how to find, create, and remove OPC object in the workspace.

Find OPC Objects in Memory

Use the opcfind function to find OPC objects in memory.

opcfind
ans =
     []

Create OPC Objects

Create some OPC objects.

da = opcda('localhost', 'Dummy.Server.1');
grp = addgroup(da);
itm1 = additem(grp, 'Fake.Item.ID1');
itm2 = additem(grp, 'Fake.Item.ID2');

Find all valid objects.

allOPC = opcfind
allOPC = 
    [1x1 opcda]    [1x1 dagroup]    [1x1 daitem]    [1x1 daitem]

The information is returned in a cell array, because opcfind can locate different objects. Use cell indexing to access an object.

foundGrp = allOPC{2}
foundGrp =
Summary of OPC Data Access Group Object: group1
   Object Parameters
      Group Type   : private
      Item         : 2-by-1 daitem object
      Parent       : localhost/Dummy.Server.1
      Update Rate  : 0.5
      Deadband     : 0%
   Object Status
      Active       : on
      Subscription : on
      Logging      : off
   Logging Parameters
      Records      : 120
      Duration     : at least 60 seconds
      Logging to   : memory
      Status       : Waiting for START.
                     0 records available for GETDATA/PEEKDATA

Pass property/value pairs to the opcfind function to find objects with a specific property.

allDA = opcfind('Type', 'opcda')
allDA = 
    [1x1 opcda]

Remove Objects From Memory

To delete an OPC object from memory, use the delete function with the object. Deleting a client object deletes all group and item objects associated with the client. Deleting a group deletes all items in that group.

delete(grp)

Find all remaining valid objects.

allOPC = opcfind
allOPC = 
    [1x1 opcda]

Using the delete function with the object will remove the object from the OPC engine but not from the MATLAB® workspace. To remove an object from the MATLAB workspace use the clear function.

Display the current workspace.

whos
  Name          Size            Bytes  Class      Attributes

  allDA         1x1               690  cell                 
  allOPC        1x1               690  cell                 
  ans           0x0                 0  double               
  da            1x1               630  opcda                
  foundGrp      1x1               630  dagroup              
  grp           1x1               630  dagroup              
  itm1          1x1               630  daitem               
  itm2          1x1               630  daitem               

Since an object was deleted, it is no longer valid.

grp
grp =
Invalid dagroup object.
This object should be removed from your workspace using CLEAR.

The items contained by that group are also invalid.

itm1
itm1 =
Invalid daitem object.
This object should be removed from your workspace using CLEAR.

Clear the associated variables.

clear grp itm1 itm2

Display the current workspace.

whos
  Name          Size            Bytes  Class      Attributes

  allDA         1x1               690  cell                 
  allOPC        1x1               690  cell                 
  ans           0x0                 0  double               
  da            1x1               630  opcda                
  foundGrp      1x1               630  dagroup              

To remove all OPC objects from the engine and to reset the toolbox to its initial state, use the opcreset function.

Note: Using the opcreset function will only delete objects from memory, not clear them from the MATLAB workspace.

opcreset

Verify that no objects remain.

allOPC = opcfind
allOPC =
     []

Variables associated with deleted objects still remain.

whos
  Name          Size            Bytes  Class      Attributes

  allDA         1x1               690  cell                 
  allOPC        0x0                 0  double               
  ans           0x0                 0  double               
  da            1x1               630  opcda                
  foundGrp      1x1               630  dagroup              

You can remove those variables using the clear function.