www.gusucode.com > opc 案例源码 matlab代码程序 > opc/demoUA_data.m
%% Visualize and Preprocess OPC UA Data % This example shows you how to work with OPC UA Data objects. % % You create OPC UA Data objects when you read historical data from an OPC UA server. OPC UA Data objects allow you to % store, visualize and manipulate historical data before converting that data to builtin data types for further % processing in MATLAB. % % For more information on generating OPC UA Data objects, see the example <docid:opc_examples.example-ex41415941>. % Copyright 2015 The MathWorks, Inc. %% Load Sample OPC UA Data Set % Load the sample data into the workspace. load demoUA_SampleData %% Display OPC UA Data objects % Examine the workspace to see what variables have been loaded. whos %% % Display a summary of the sample data. summary(dataSample) %% % The data object contains three data sets. The first element |Double| contains 9 values, the second and third have 12 values each. % % See whether the |Float| and |Int32| data sets have the same timestamp. arrayHasSameTimestamp(dataSample(2:3)) %% % Display the |Float| and |Int32| data sets together. Because all the elements have the same timestamp, a table of values can be displayed dataSample(2:3) %% Change the Date Display Format % Get the current date display format using |opc.getDateDisplayFormat|. origFormat = opc.getDateDisplayFormat; %% % Change the display format to standard US date format and display the value again. opc.setDateDisplayFormat('mm/dd/yyyy HH:MM AM'); dataSample(2:3) %% % Reset the display format to the default. opc.setDateDisplayFormat('default') %% % Reset the display format to the original value. opc.setDateDisplayFormat(origFormat); %% Visualize OPC UA Data % Visualize OPC UA Data using the |plot| or |stairs| methods on the data object. axH1 = subplot(2,1,1); plot(dataSample); title('Plot of sample data'); axH2 = subplot(2,1,2); stairs(dataSample); title('Stairstep plot of sample data'); legend('Location', 'NorthWest') %% Resample OPC UA Data % The data in the |dataSample| set has different timestamps. arrayHasSameTimestamp(dataSample) %% % Attempt to convert the data to a double array. The conversion will fail. try vals = double(dataSample); catch exc disp(exc.message) end %% % The intersection of the data timestamps results in a smaller data set containing the common timestamps from all % elements. dataIntersect = tsintersect(dataSample) %% % Convert the data object into a double array. vals = double(dataIntersect) %% % Use |tsunion| to return the union of time series in a Data object. New values are interpolated using the method % supplied (or linear interpolation if no method is supplied). The quality is set to "Interpolated" for those new % values. dataUnion = tsunion(dataSample) %% % Plot the data with markers to show how the methods work. subplot(2,1,1); plot(dataSample, 'Marker','.'); hold all plot(dataIntersect, 'Marker','o', 'LineStyle','none'); title('Intersection of time series in Data object'); subplot(2,1,2); plot(dataSample, 'Marker','.'); hold all plot(dataUnion, 'Marker','o', 'LineStyle','--'); title('Union of time series in Data object'); %% % Resample the small data set at specified time steps. newTS = dataSample(1).Timestamp(1):seconds(5):dataSample(1).Timestamp(end); dataResampled = resample(dataSample,newTS) figure; plot(dataSample); hold all plot(dataResampled, 'Marker','x', 'Linestyle','none'); %% Filter Data by Quality % Find only the Good data from the second element of resampled data set resampledGood = filterByQuality(dataResampled(2), 'good') %% % Filter the second element of the resampled data to return only the Interpolated data. Visualise the filtered data with % the original. resampledInterpolated = filterByQuality(dataResampled(2), 'Origin','interpolated') figure; plot(dataResampled(2)) hold on plot(resampledGood, 'Marker', '+', 'Linestyle','none', 'DisplayName', 'Good'); plot(resampledInterpolated, 'Marker','x', 'Linestyle','none', 'DisplayName', 'Interpolated'); legend('Location', 'NorthWest')