Unable to Cast a Matlab COM object in C#

4

Hello everyone I'm trying to control Matlab from a C# application, so I went for the COM server solution however I can't make it work.

First, I registered my Matlab as COM server using comserver('register','User','all') in the Matlab Command Window. I then referenced this server in my C# project.

After that I implemented simple C# code to access a Matlab function that I wrote :

        // Create the MATLAB instance 
        MLApp.MLApp matlab = new MLApp.MLApp(); // Where the code returns an error

        // Change to the directory where the function is located 
        matlab.Execute(@"cd C:\Users\jar\Downloads\");

        // Define the output 
        object result = null;

        // Call the MATLAB function myfunc
        matlab.Feval("traj2D_image",1, out result,
                                        ParametresBalayage.NomFichier,
                                        ParametresBalayage.LongueurBalayage,
                                        ParametresBalayage.NbBalayage,
                                        ParametresBalayage.PasBalayage,
                                        ParametresBalayage.DecalageBalayage,
                                        ParametresBalayage.DecalageStries,
                                        ParametresBalayage.Vitesse,
                                        sens);

        // Display result 
        object[] res = result as object[];

But I get this error when I try to create the MATLAB instance :

System.InvalidCastException : 'Unable to cast a COM object of type 'System.__ComObject' into interface type 'MLApp.MLApp'. This operation failed, because calling QueryInterface on the COM component for the interface with the IID '{669CEC93-6E22-11CF-A4D6-00A024583C19}' has failed because of the following error : Unspecified error (HRESULT Exception : 0x80004005 (E_FAIL)).'

This is weird because the Matlab instance is created, a new Matlab Command Window opens, but the code doesn't go further and stops.

Does anyone know why I have this problem? Thank you in advance.


EDIT : trying another method

I followed step by step the MathWorks' instructions but it still gives me the same error when creating the MLApp instance so I tried to explore other options such as manually register, unregister, or query MATLAB COM server but nothing changed. I still want the first option to work but I went to see other .NET languages to see how they were implementing that.

So I'm trying another method that I found in VB.NET and that works in an excel VBA module : Type mlType; Object matlab;

        mlType = Type.GetTypeFromProgID("Matlab.Application");
        matlab = Activator.CreateInstance(mlType);

        Console.WriteLine(matlab.???("surf(peaks)")); // what do i have to put here?

This creates an instance of MAtlab without returning any error however in VB.NET there is an Execute class in objects that I don't have here so I tried matlab.Equals("surf(peaks)") (which is obviously not the right method) but I don't know what to put to get the instance to do what I want.

c#
matlab
com
asked on Stack Overflow Feb 5, 2021 by JackA • edited Feb 8, 2021 by JackA

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0