Read and Write Current OPC UA Server Data

This example shows you how to read and write data to an OPC UA server.


Install an OPC UA Simulation Server for OPC UA Examples

Create a Client and Connect to the Server

You create client objects using the results of a query to the Local Discovery Service using opcuaserverinfo, or directly using the host name and port number of the server you are connecting to. In this case, use the host and port number syntax.

uaClient = opcua('localhost',51210);

Find the Double, Float, and Int16 nodes in the Static.Scalar namespace.

staticNode = findNodeByName(uaClient.Namespace,'Static','-once');
scalarNode = findNodeByName(staticNode,'Scalar','-once');
doubleNode = findNodeByName(scalarNode,'DoubleValue');
floatNode = findNodeByName(scalarNode,'FloatValue');
int16Node = findNodeByName(scalarNode,'Int16Value');
nodes = [doubleNode,floatNode,int16Node];
1x3 OPC UA Node array:
    index     Name      NsInd  Identifier  NodeType  Children
    -----  -----------  -----  ----------  --------  --------
      1    DoubleValue  2      10226       Variable  0
      2    FloatValue   2      10225       Variable  0
      3    Int16Value   2      10219       Variable  0

Read Values from Nodes

Use readValue to read the current value of a node. You can query the Value, the Timestamp when the Value was updated, and the Quality associated with the value when written.

[v,t,q] = readValue(uaClient,nodes);

When you read from multiple nodes, the Values are returned as a cell array. The class of the data on the server is preserved as much as possible.

    [ 2.2933e-35]
    [     -29737]


The timestamp is returned as a MATLAB® datetime variable. It represents the time when the source provided the value to the server.

   12-Apr-2016 16:48:11
   12-Apr-2016 16:48:11
   12-Apr-2016 16:48:11

The quality is returned as an OPC UA Quality, which displays as a text description.

OPC UA Quality ID:

You can interrogate the Quality to determine characteristics of the returned quality. In this example, the quality is good.


The value is not interpolated, but is a raw value (stored by the server directly from the sensor).

interpolated = isInterpolated(q)
raw = isRaw(q)
interpolated =


raw =


Write Data to Nodes

You can write data to any scalar node. When you write to multiple nodes, you must pass a cell array of values, one for each node to be written.

newValues = {12,65,-4};

To verify that the values were written correctly, and retrieve the value again.

serverValues = readValue(uaClient,nodes)
serverValues = 


You can update values directly within the cell array and write them back to the server.

serverValues{2} = serverValues{2} + 1;

Read and Write Values With a Single Node

When working with a single node, you receive and can pass and the value directly, without using a cell array.

dblValue = readValue(uaClient, doubleNode)
writeValue(uaClient, doubleNode, 12)
newDbl = readValue(uaClient, doubleNode)
dblValue =


newDbl =


Reading and Writing to Nodes Directly

You can write and read directly from the node variable, as long as that node was created from the client (using the Namespace property or browseNamespace) or you passed a client to the opcuanode function when creating your node variable.

[vals,ts,qual] = readValue(nodes)
vals = 


ts = 

   12-Apr-2016 16:48:11
   12-Apr-2016 16:48:11
   12-Apr-2016 16:48:11

qual = 

OPC UA Quality ID:

Disconnect from Server

When you have finished communicating with the server, disconnect the client from the server. This is also automatically performed when the client variable goes out of scope in MATLAB.