Main Content

subscribe

Monitor nodes for data change notification

Since R2023b

    Description

    example

    SubscrObj = subscribe(UaClient,NodeList,DataChangeFcn) creates a subscription request with the specified NodeList for the UaClient. When the server sends notification of a data value change on any of the nodes, the specified DataChangeFcn is triggered to execute in MATLAB®.

    SubscrObj = subscribe(UaClient,NodeList,{DataChangeFcn,UserData}) provides additional data for the DataChangeFcn function to use. Its type and format depends on the function.

    SubscrObj = subscribe(___,PublishInterval=PubIntv) specifies a periodic rate of notification, PublishInterval, as PubIntv seconds. This controls the rate at which the server monitors the node values, sending a notification, indicating if any changes have occurred. The default is 1 second.

    Examples

    collapse all

    Create an OPC UA client, and subscribe to a list of nodes.

    UaClient = opcua('localhost',51210);
    connect(UaClient);
    NodeList = browseNamespace(UaClient);  % Manually select nodes.
    SubscrObj = subscribe(UaClient,NodeList,@mycallback,PublishInterval=3);

    Subscribe while providing custom arguments to the callback function.

    SubscrObj = subscribe(clientObj,nodes,{@mycallback,UserData})

    Input Arguments

    collapse all

    OPC UA client specified as an opc.ua.Client object. You can create the client with the opcua function.

    Example: UaClient = opcua(...)

    Data Types: object

    List of nodes to be monitored, specified as an array of node objects. For information on node object functions and properties, type

    help opc.ua.Node

    Example: NodeList = browseNamespace(UaClient)

    Data Types: object

    Function to execute at data change notification from subscribed nodes, specified as a function handle. Typically, this function assesses the changed data sent from the server. The input arguments to the function are the subscription object, the notification object, and the optional UserData. The notification object, provided by the server, includes Node and Data properties that include the changed nodes and values.

    Example: @mycallback

    Example: {@mycallback,UserData}

    Data Types: function_handle

    Periodic notification rate, specified in seconds as a double. The default is 1 second. This is the rate at which the server monitors variable value changes on the nodes. When a change is detected, notification is sent to the client indicating the change, triggering the callback.

    Example: PublishInterval=0.1

    Data Types: double

    Output Arguments

    collapse all

    Subscription, returned as an opc.ua.Subscription object, with the following properties.

    PropertyDescription
    ServerOPC UA server name
    NodesList of OPC UA nodes to monitor in subscription
    IDNumerical ID of this subscription
    EnabledIndicator of subscription on or off
    PublishIntervalNotification rate in seconds
    DataChangedFcnCallback function to execute upon notification of data change

    Version History

    Introduced in R2023b