3 Replies Latest reply on Jul 19, 2018 7:49 AM by Jiaqi

    how to use AFCalculation.PercentTrue in Matlab

    Jiaqi

      Dear all,

      I'm retrieving information on a sampled data tag on PI. I'm using Matlab to perform the job.

      I found the example in Error in AFCalculation.PercentTrue  -  gives always 100 percent true results  So I tried to write the following in Matlab:

      ===

      NET.addAssembly('OSIsoft.AFSDK');

      import OSIsoft.AF.*

      import OSIsoft.AF.Asset.*;

      import OSIsoft.AF.Time.*;

      import OSIsoft.AF.Data.*;

      import OSIsoft.AF.PI.*;

       

      piservers = OSIsoft.AF.PI.PIServers;

      pisrv = piservers.DefaultPIServer;

      timerg = AFTimeRange('01-07-2018 00:00:00','02-07-2018 00:00:00'); % dd-mm-yyyy

      inter = AFTimeSpan(0,0,0,1.0);    % 1hr interval

      inter2 = AFTimeSpan(0,0,0,0,1.0);   % 1min interval

      percentGood = AFValues();

      strFilter = strcat('''sinusoid''','>200');

      results = AFCalculation.PercentTrue(pisrv, strFilter, timerg,...

          inter, inter2,AFTimestampCalculation.Auto, percentGood);

      ====

       

      However, the code doesn't work.

      I'm not sure about the following:

      - syntax of filter expression, i.e., strFilter in the code. My actual tag on a real server indicates the status of an instrument, the value will be either 'On' or 'Off'. The tag name is Device1_On_Off. In Excel,  it is PITimeFilter("'Device1_On_Off'= ""On""",...).  I need to calculate to percentage of 'On' time. If I want to implement in Matlab, can I write like

      strFilter = strcat('''Device1_On_Off''=','""On""');     ?

      so I will get a filter expression like:

      'Device1_On_Off'=""On""

      which resembles the expression in Excel.

       

      - percentGood as the out parameter: is the last line correct in Matlab?

       

      I also tried

      methods(AFCalculation) in command line, however, I received the following error message:

      "

      Error using OSIsoft.AF.Data.AFCalculation

      Abstract classes cannot be instantiated.  Class 'OSIsoft.AF.Data.AFCalculation' is

      declared as Abstract.

      "

      does this mean I cannot use methods of AFCalculation in Matlab? I have no problem when checking e.g., methods(AFValues).

       

      Thank you.

        • Re: how to use AFCalculation.PercentTrue in Matlab
          tramachandran

          When you mention "the code doesn't work", is it not working as expected or does it throw an error.exception? Could you provide more details.

          The function AFCalculation.PercentTrue returns two sets of values, Out parameter indicating the percent good of each calculation interval value and returns an AFValues collection with each value containing the percentage of time the expression was true over each calculation interval.

           

          For: Error using OSIsoft.AF.Data.AFCalculation Abstract classes cannot be instantiated.  Class 'OSIsoft.AF.Data.AFCalculation' is declared as Abstract.

          Class method names - MATLAB methods , methods() = Get Method Names from Object. Could you try methods AFCalculaltion instead.

           

          You can also give Announcing PI Web API client library for MATLAB Toolbox a try.

            • Re: how to use AFCalculation.PercentTrue in Matlab
              Jiaqi

              the code gives the following error message:

              ===

              No method 'PercentTrue' with matching signature found for class

              'OSIsoft.AF.Data.AFCalculation'.

              Error in TestFileSampledSingle (line 26)

              results = AFCalculation.PercentTrue(pisrv, strFilter, timerg,...

              ===

              The "no matching signature" error is explained by Matlab as "this error occurs when you call a function without the correct input or output arguments, or if there is an error in the function signature in the header file.". It seems to me somthing wrong with the input, very likely associated with filter expression and/or the last out parameter percentGood.

               

              Yes, methods AFCalculaltion works! Thanks. I can see methods of AFCalculation as below.

              ===

              Methods for class AFCalculation:

              Equals                     GetType                   

              GetHashCode                ToString                  

              Static methods:

              CalculateAtIntervals       ConvertExpressionTarget    ReferenceEquals           

              CalculateAtRecordedValues  ConvertFilterTarget       

              CalculateAtTimes           DetermineEvaluationServer 

              CalculateSummaries         PercentTrue

              ====

              • Re: how to use AFCalculation.PercentTrue in Matlab
                Jiaqi

                Thanks, I used methodsview AFCalculation

                In Matlab, the inputs required are slightly different from syntax examples. the last out parameter is not required in Matlab.

                i.e., the last line shall be

                results = AFCalculation.PercentTrue(pisrv, strFilter, timerg,...

                    inter, inter2,AFTimestampCalculation.Auto);

                 

                instead of

                results = AFCalculation.PercentTrue(pisrv, strFilter, timerg,...

                    inter, inter2,AFTimestampCalculation.Auto, percentGood);

                 

                It works now.

                cheers

                t