Unable to Cast a Matlab COM object in C#

7 次查看(过去 30 天)
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 (the same as what's shown in the documentation) 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\jarnoldi\Downloads\");
// Define the output
object result = null;
// Call the MATLAB function myfunc
matlab.Feval("traj2D_image",1, out result, ParametresBalayage.NomFichier);
// 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.

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write COM Applications to Work with MATLAB 的更多信息

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by