How to Quit MATLAB using C# & COM?

조회 수: 16 (최근 30일)
Shawn
Shawn 2011년 8월 9일
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?

채택된 답변

Friedrich
Friedrich 2011년 8월 9일
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();
  댓글 수: 3
Conrad
Conrad 2011년 12월 1일
Hi Shawn, did you make any progress on this...having the same problem.
Friedrich
Friedrich 2011년 12월 3일
I created a C# project where I added the ML Type Library as reference (win 7 64bit, R2011b). I used the following code which worked fine:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
class Program
{
static void Main(string[] args)
{
Type mlt = Type.GetTypeFromProgID("Matlab.Application.Single");
MLApp.MLApp ml = (MLApp.MLApp)Activator.CreateInstance(mlt);
ml.Visible = 1;
//wait 5 seconds
System.Threading.Thread.Sleep(5000);
ml.Quit();
System.Console.WriteLine("Press any key to close this appl.");
System.Console.Read();
}
}
}
Which code do you use in order to get this behavior where you can't close MATLAB?

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Fangjun Jiang
Fangjun Jiang 2011년 8월 9일
Here is what I found in the document. I couldn't test it though whether ml.Quit works or not.
ml.Quit
ml.delete
Example — Calling MATLAB from a C# Client
This example creates data in the client C# program and passes it to MATLAB. The matrix (containing complex data) is then passed back to the C# program.The reference to the MATLAB Type Library for C# is:
MLApp.MLAppClass matlab = new MLApp.MLAppClass();
Here is the complete example:
using System;
namespace ConsoleApplication4
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
MLApp.MLAppClass matlab = new MLApp.MLAppClass();
System.Array pr = new double[4];
pr.SetValue(11,0);
pr.SetValue(12,1);
pr.SetValue(13,2);
pr.SetValue(14,3);
System.Array pi = new double[4];
pi.SetValue(1,0);
pi.SetValue(2,1);
pi.SetValue(3,2);
pi.SetValue(4,3);
matlab.PutFullMatrix("a", "base", pr, pi);
System.Array prresult = new double[4];
System.Array piresult = new double[4];
matlab.GetFullMatrix("a", "base", ref prresult, ref piresult);
}
}
}
  댓글 수: 1
Shawn
Shawn 2011년 8월 9일
I get the following message when adding ml.delete:
'MLApp.MLApp' does not contain a definition for 'delete'

댓글을 달려면 로그인하십시오.


Moe
Moe 2012년 9월 18일
I use:
public static MLApp.MLApp matlab;
matlab = new MLApp.MLApp();
matlab.Execute("exit");

Andrea Di Domenico
Andrea Di Domenico 2013년 6월 18일
I use this code:
try
{
matlab.Execute("quit");
}
catch (Exception ex)
{
}
It is not so clean but it works

카테고리

Help CenterFile Exchange에서 MATLAB Compiler SDK에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by