MATLAB: How to Quit MATLAB using C# & COM

ccomMATLABquit

I have a C# program that invokes a new dedicated Matlab instance as follows:
Type mlt = Type.GetTypeFromProgID("Matlab.Application.Single");
MLApp.MLApp ml = (MLApp.MLApp)Activator.CreateInstance(mlt);
However, I am unable to get the Matlab instance to quit. I have tried ml.Quit();, but the instance remains running. I have also tried ml.Execute("quit");, but I get a .NET error. Is there another way or more I need to do to get Matlab to quit?

Best Answer

Hi,
Fangjun is right. It seems like you are using late binding. This makes coding a bit more complicated. Why aren't you using early binding? To do early binding add as reference to your C# projekt the COM refernce named Matlab Application (Version 7.**) Type Library. And than as code simply do:
MLApp.MLApp ML = new MLApp.MLApp();
ML.PutWorkspaceData("test", "base", 4);
String output = ML.Execute("whos");
System.Console.Write(output);
// keep application running after some keystroke
System.Console.Read();
ML.Quit();