3 Replies Latest reply on Aug 26, 2010 2:40 AM by david@realiv

    A COMException Error when invoke MATLAB in ACE Program




      I was going to invoke MATLAB functions in my ACE program, while I made a test acorrding to white paper in vCampus Library, the code is as below(exactly the same as in white paper, VB.NET):

      Imports OSIsoft.PI.ACE

      Public Class MATLAB_Test_Module
          Inherits PIACENetClassModule
          Private sinusoid As PIACEPoint
          Private cdt158 As PIACEPoint
          Private oMatLab As MLApp.MLApp
          '      Tag Name/VB Variable Name Correspondence Table
          ' Tag Name                                VB Variable Name
          ' ------------------------------------------------------------
          ' cdt158                                  cdt158
          ' sinusoid                                sinusoid
          Public Overrides Sub ACECalculations()
              Dim oOutPut As Object
              oMatLab.PutWorkspaceData("x", "base", cdt158.Value)
              oMatLab.GetWorkspaceData("y", "base", oOutPut)

              sinusoid.Value = oOutPut
          End Sub

          Protected Overrides Sub InitializePIACEPoints()
              cdt158 = GetPIACEPoint("cdt158")
              sinusoid = GetPIACEPoint("sinusoid")
          End Sub

          ' User-written module dependent initialization code
          Protected Overrides Sub ModuleDependentInitialization()
              oMatLab = New MLApp.MLApp()
          End Sub

          ' User-written module dependent termination code
          Protected Overrides Sub ModuleDependentTermination()
              oMatLab = Nothing
          End Sub
      End Class

      But when I tried to debug, it came to a COMException error on the Line I marked. It seems object oOutPut is used before being set value, while I was just following the code in white paper 


      Here's my Error detail:

             at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)    at MLApp.MLAppClass.GetWorkspaceData(String Name, String Workspace, Object& pdata)    at PIACE.MATLAB_Test.MATLAB_Test_Module.ACECalculations() in C:\Program Files\pipc\ACE\ClassLibraries\MATLAB_Test\MATLAB_Test_Module.vb:line 19    at PIACEClassLibraryHost.PIACEHost.PIACENetSchedule..ctor(String ServerName, String PIACEClassLibraryName, String ACEModuleName, String ACEContext)


      Could anyone give me some advise on how to revise the code to make it work? Thanks

        • Re: A COMException Error when invoke MATLAB in ACE Program

          I had to make two changes to make your example work in my environment, the first was using

          oOutPut=oMatLab.GetVariable("y", "base")
          instead of
          oMatLab.GetWorkspaceData("y", "base", oOutPut)


          The second was related to your AddOne function, that I am not familiar with and do not have on my system. Is it something custom that you wrote? Does it work when you interactively use it within MATLAB itself? Changing from

          to any other valid MATLAB expression causes everything to work normally, e.g.


          Any MATLAB expression balks at the GetWorkspaceData approach in my VS 2010 / MATLAB R2010a environment.

            • Re: A COMException Error when invoke MATLAB in ACE Program
              Ahmad Fattahi

              I suspect that might be a versioning issue. I used Visual Studio 2008 and MATLAB R2010a; GetWorkspaceData works just fine for me.


              David, just for the sake of testing, can you give oOutPut a value manually before using it in GetWorkspaceData and see what happens?


              I will have a presentation in this year's vCampus Live! event covering 3rd party analytics and PI which will include MATLAB for the most part. Looking forward to seeing you there!

                • Re: A COMException Error when invoke MATLAB in ACE Program

                  Hi Ahmad,

                  I’m currently using VS2008 and MATLAB R2009a, the same as in white paper.

                  Thanks for your advices and I’m looking forward to your presentation


                  I think the problem is on MATLAB but not ACE program, since I’m a novice on MATLAB, I forgot to put my m file into MATLAB default folder. It worked when I used it under indicated folder in MATLAB but seems could not be properly invoked in ACE. And now it has been fixed.

                  Thanks for your suggestion and GetVariable also works for me.