Main Content

Manipulate Data Using OPC HDA Objects

OPC HDA data objects provide initial data storage, visualization, and manipulation functions for you to work with OPC historical data in MATLAB®. To facilitate preparation for further processing, OPC HDA data objects allow you to resample OPC historical data as follows:

  • To prepare data for analysis algorithms that require data to be regularly sampled, use the resample function.

  • To ensure that data from all items contains the same timestamp vector, use the tsunion function, which keeps all data and interpolates data for missing timestamps in each item, or the tsintersect function, which discards any data from a timestamp that does not exist in all items in the object.

Resample Data Objects to Include All Available Time Stamps Using tsunion

Given an array of data objects, tsunion adapts all data to have a single common set of timestamps by finding all unique time stamps in all items of the array. The values of each data item are then extrapolated or interpolated at the new timestamps. Resampling is performed using the method specified in the function call. Valid methods are 'linear', 'spline', 'pchip', 'nearest', and 'hold'. The default is 'linear'. If any returned Value is a character vector, only 'hold' is supported. Elements with the same item ID are combined, so that tsunion creates data objects with unique item IDs. The Quality of interpolated timestamps is set to 'Interpolated:Good', and for extrapolated timestamps is set to 'Interpolated:Uncertain'.

Plots of separate data object and their union

The top two plots above depict two separate data objects. The bottom plot is the result of these two data objects being passed to the tsunion function. You can see that in the bottom plot that each element has been extended to include the timestamps of the other and that values have been extrapolated to satisfy these new timestamps.

Resample Data Objects to Include All Common Time Stamps Using tsintersect

When you are interested in only the timestamps common to a number of data objects, you can use the tsintersect function. It generates a new OPC HDA data object in which each element has the same timestamp vector composed of those timestamps that were common to all items in the original data objects provided. If the provided data objects contain elements with the same item ID, those elements are combined into one before computing the intersection.

Plots of separate data object and their intersection

The previous figure shows how the values of two data objects, plotted in the first and second positions respectively, can be intersected to produce a new object whose elements contain only timestamps common to the original two. Uncommon timestamps are discarded along with their data values.

Resample Data to a New Set of Time Stamps

You might want to resample all items in a data object at specified time stamps; for example, when you have data values for a second item and want to correlate your data object with the original at the same timestamps. Where no exact values are available, the resample function resamples (interpolate or extrapolate) the data values at the requested time stamps using the resampling method you specify. Valid methods include 'linear', 'spline', 'pchip', and 'nearest' (see interp1 for details on these methods), as well as 'hold', which implements a zero-order-hold behavior (previous values are held until a new value exists).

For character vector values, only the 'hold' method is supported. Trying to resample data containing character vectors with any method other than 'hold' generates an error.

This concept is illustrated in the following graphic.

Plots of original and resampled data with different timestamps

In this figure, the blue line represents the original data values while the red line represents the resampled data at a new set of timestamps. These new timestamps are marked by red stars while the original timestamps are marked by blue circles.

Convert OPC HDA Data Objects to MATLAB Numeric Data Types

When retrieving data from the server and storing it in an OPC data object, the client automatically converts the values from the OPC variant types (see Comparison of MATLAB and COM Variant Data Types). Retrieve the data values from the data object by referencing the Value property. For example, to display and access the first element of the hdaReadRaw data object:

hdaReadRaw
hdaReadRaw = 
1-by-5 OPC HDA Data object:
        ItemID            Value            Start TimeStamp           End TimeStamp              Quality        
    --------------  -----------------  -----------------------  -----------------------  ----------------------
    Random.Int1      5 int8 values    2010-12-01 16:05:30.902  2010-12-01 16:05:32.869  1 unique quality [Raw]
    Random.Uint2    5 double values   2010-12-01 16:05:30.902  2010-12-01 16:05:32.869  1 unique quality [Raw]
    Random.Real8    5 double values   2010-12-01 16:05:30.902  2010-12-01 16:05:32.869  1 unique quality [Raw]
    Random.String    5 cell values    2010-12-01 16:05:30.902  2010-12-01 16:05:32.869  1 unique quality [Raw]
    Random.Boolean  5 logical values  2010-12-01 16:05:30.902  2010-12-01 16:05:32.869  1 unique quality [Raw]

class(hdaReadRaw(1).Value)

int8

An alternative is to call standard type conversion methods available in MATLAB on the entire object, in which case all items are converted to the chosen type (assuming they have the same timestamp vectors):

newArray = double(hdaReadRaw(1));
class(newArray)
double

In this example, hdaReadRaw(1) has an initial native data type of 'int8', yet after passing it to the 'double' conversion call, the resulting values are of the native MATLAB type 'double'.