Effacer les filtres
Effacer les filtres

Unable to Cast a Matlab COM object in C#

6 vues (au cours des 30 derniers jours)
Jack Arnoldi
Jack Arnoldi le 8 Fév 2021
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.

Réponses (0)

Catégories

En savoir plus sur Write COM Applications to Work with MATLAB dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by