PI to MATLAB - Calculating vector averages

Discussion created by SPR_Glasgow on Jun 2, 2014
Latest reply on Jun 10, 2014 by SPR_Glasgow



I’m using MATLAB to extract data from a PI server via the PI OLEDB Provider. I would typically use an SQL query string like the one below to extract 10-min averages of a particular tag.


sql=strcat('SELECT value FROM piarchive..piavg WHERE tag = ''',tag,''' AND time BETWEEN ''',starttime,''' AND ''',finishtime,''' AND timestep = 600s')


If the tag links to directional data in degrees, I would like to calculate a 10-min vector average. Mathematically, this can be achieved by combining the 10-min averages of the SIN and COS components of the tag using ATAN2, i.e.


ATAN2(tag_sin,tag_cos) where


tag_sin = average of SIN(tag) for all records in 10-min period


tag_cos = average of COS(tag) for all records in 10-min period


I understand this might be possible using an expression, i.e. WHERE expr ... but I am unsure of the correct syntax. Also, does the tag have to be converted to radians before use with SIN and COS?


Can anyone help?


A further correction is required to ensure all averages are in the range 0-360deg, but I can do this afterwards.