Following on from the information contained in various OSIsoft PI white papers that explain the use of MATLAB for extracting, analysing and writing data to the PI database, I have spent a significant amount of time gathering information on how to implement this process at work. The method I utilised to achieve my read/write analysis through MATLAB is covered in the section of the below white paper titled, 'Using ADO and the PI OLEDB Provider'. This section introduces the PI developed MATLAB functions and m-files available for use, however, it lacks an explanation of how to implement them effectively. Specifically, I have used the PI_CompressedData function to first read data from a specified tag for a defined period ('starttime' to 'endtime'), then in MATLAB I have analysed the data and used PI_WriteValue to write the results back to a new PI Point (new PI points can be created using PI SMT for example). A simple example m-file script for retrieving data from the PI database using PI_CompressedData in MATLAB is detailed below (Please note the annotation of the below code to provide a brief explanation):


server = 'MyServerName';

tag = 'MyTagName'; %example 'sinusoid'

starttime = '25-May-2017 06:00:00 AM';

endtime = '25-May-2017 06:00:00 PM';


Output = PI_CompressedData(server,tag,starttime,endtime); %Output is an N by 3 cell containing the 'tag name', 'timestamp', and 'value' in the three columns.


Values = Output{:,3}; %As indicated in the comment above 'values' of the retrieved data stored in 'Output' are located in the third column, the curly braces '{}' indicate that you're extracting data from a cell and that you want the new variable to store it as an array.


TimeStamp = Output{:,2}; %Again, the 'timestamp' for the data is stored in the second column.


%At this point you would complete your analysis on the array call 'Values', for example:


newValues = Values + 1; %Simply add 1 to all values stored in the array 'Values'

Once you have the newValues array you need to prepare the array for storing into PI using the PI_WriteValue function. The PI_WriteValue function accepts strings of information while the PI_CompressedData function extracts numerical and strings of information. Conversion of the newValues array to a cell containing a number of strings is required. Unlike PI_CompressedData where we were able to retrieve a number of data points from the PI database with a single call, a drawback of the PI_WriteValue function is that it can only write a single data point to a single timestamp at any given time and therefore a loop needs to be utilised in order to store an array of data into PI. In addition, when writing data to PI it is much faster if you open you connection, write all the data to PI and then close the connection as opposed to opening and closing the connection every time you write a piece of information to PI. All of this can be achieved using the following example:


%create the new tag

newtag = 'MyNewTagName'; %example 'Newsinuoid', this tag needs to be created before PI_WriteValue will execute and write the data to PI.


LenA = length(Values); %This variable is used to define the end of the for loop below.

Result = zeros(1,LenA); %This array will store the resulting binary success = 1 or failure = 0 for the PI_WriteValue function.


%Use the following code to build a connection string; it has been taken from inside the PI_CompressedData or PI_WriteValue functions.

cnstr = strcat('Provider=PIOLEDB.1;Data Source=',server,';Persist Security Info=False;');



%Open connection, opening the connection outside of the PI_WriteValue function and passing it in ensures that the connection remains open and that data will be stored to the PI database more efficiently.


cn=adodbcn(cnstr);     %cnstr is defined above.


for i = 1:LenA


     newValuesString = num2str(newValues(i));     %convert the current newValue(i) from a number to a string and store it in the variable handle newValueString.

     Result(i) = PI_WriteValue(server,newtag,TimeStamp{i},newValuesString,cn);     %Execute the PI_WriteValue function ensuring to pass one timestamp and the new value as a string, as well as passing in the connection 'cn' so that the function knows not to reopen/open a connection.




%Close connection, also taken from PI_CompressedData/PI_WriteValue functions.


White Paper - Using PI Data with MATLAB