Main Content

subscribe

Monitor nodes for data change notification

Since R2023b

    Description

    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.

    example

    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, see 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: SubObj = subscribe(UaClient,NodeList,@mycallback,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