I’m looking to read a large number of nodes from an OPC_UA server using
[val] = readValue(uaClient,read_nodes_test)
For tidiness sake, i’ve got my nodes stored in a struct which i’ve called OPC_UA_nodes. The issue is i cannot find a way to convert this struct of nodes to the node array required for the readvalue command.
All documentation encourages me to use struct2cell and then cell2mat, however cell2mat does not support cell arrays containing cell arrays or objects.
What is the best way to create the node array? Besides
nodes = [node1, node2, node3,..., node75]
2
You can use structfun
to iterate over all of the fields in OPC_UA
, and if the function you supply is just @(x)x
then the node within that struct field will be returned. Since they’re all scalar objects which can be concatenated into an array, they will be.
Example setup:
OPC_UA = struct();
OPC_UA.node1 = opcuanode(1, 'node1');
OPC_UA.node2 = opcuanode(2, 'node2');
OPC_UA.node3 = opcuanode(3, 'node3');
Using structfun
to get all of the nodes into an array:
nodes = structfun( @(x)x, OPC_UA );
A more verbose way of doing this which would let you be selective about which fields of OPC_UA
you use would be something like this, looping over the field names with dynamic struct.(fieldname)
syntax:
fn = fieldnames( OPC_UA );
nodes = opc.ua.Node.empty(0,1);
for i = 1:numel(fn)
nodes(i,1) = OPC_UA.(fn{i});
end