My first blog post; finally, only took me 2 months to write this!

 

Numerous method of accessing PI data from Matlab have been published so a thought I throw another one into the mix. The issue with most of these methods is that support for AF is limited, particularly if you want to write back. The good news is that it turns out that Matlab is very capable of calling the AF SDK (or any .NET assembly for that matter). This means that you can use the very rich AF SDK to interact with AF from Matlab. AF 2.5 also gives you access to all the PI data. So I thought that I would show the basics, the code is not "production" ready seeing that I have not included minor things like exception handling.

 

Why would you want to do this:

 

Matlab is an excellent tool for number crunching. Building model and generally manipulating large data sets is really easy in Matlab.  I'm guessing you all know what the PI Systems is (otherwise you probably wouldn't be here). Using the AF SDK has numerous advantageous over other method:

  • I've found the performance to be superior to OLE DB
  • No limitation: you can read/write/... Anything that the AF SDK supports is available to you.

Useful link:

The real stuff

The first step is to create an instance of the AF SDK and import the relevant namespaces (note the .* at the end of the namespaces). Yes you do need the AF SDK on the Matlab computer.  

 
afSDK = NET.addAssembly('OSIsoft.AFSDK');
import OSIsoft.AF.*
import OSIsoft.AF.Asset.*

 Now connect to an AF server and database:

 
piSystems = OSIsoft.AF.PISystems;
PIsystem = piSystems.Item('MyAFServer');
AFdatabase = PIsystem.Databases.Item('MyAFDatabase');

 

 

 That is it. You now have a fully functional connection to AF via the SDK within Matlab. This means that you can do anything with this connection that the AF SDK allows.

 

Couple of examples working with the AF:

Find an Element: 

afElements = AFElement.FindElements(AFdatabase, [], 'Amplats', AFSearchField.Name, true, AFSortField.Name,  AFSortOrder.Ascending,intmax('int32'));

You hopefully noticed the one trick, nulls are passed as []. Otherwise it is pretty much the same as working with the AF SDK in C#. Below is a simply example of access the returned elements collection

 

3821.AFElementSearch1.png

Get some values: 

powerTransmitters = AFElement.FindElements(AFdatabase, [], '138JI006', AFSearchField.Name, true, AFSortField.Name,  AFSortOrder.Ascending,intmax('int32'));
attrib = powerTransmitters.Item(0).Attributes;
PV = attrib.Item('PV');

myTime = OSIsoft.AF.Time.AFTime.Now;
myValues = PV.Data.RecordedValue(myTime, Data.AFRetrievalMode.AtOrBefore, PV.DefaultUOM);

 3021.AFValues.png

 

The more astute reader will have noticed that I went and got another element; I need one that had actual values. You will also notice that I constructed an AFTime object; you must pass a time object to the RecordValue method and a Matlab type doesn't cut it.

 

I think I've demonstrate just how easy it is to work with the AF SDK directly within Matlab. It very powerful option for access AF (and now PI data thanks to RDA) from Matlab.

 

All the code was tested on Matlab 2013a. You will need to check the MathWorks documentation if you are using an older version.

 

So that all folks