前段時間琢磨了一下如何用c#代碼使用matlab,覺得頗有收獲,貼出;來共享一下。
需要引入Interop.MLApp.dll

private Type matlabtype;

private object matlab;

object[] arrayInput;



/**//// <summary>

/// 開始計算

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void beginCompute_Click(object sender, EventArgs e)


...{

this.richTextBoxResult.Text = "";

try


...{

string formula = null;


matlabtype = Type.GetTypeFromProgID("matlab.application");



matlab = Activator.CreateInstance(matlabtype);


formula = this.richTextBox1.Text + " ";


arrayInput = new Object[] ...{ formula };

func();


//ThreadStart ts = new ThreadStart(func);

//Thread th = new Thread(ts);

//th.Start();

}

catch


...{

}

}

private void func()


...{

this.richTextBoxResult.Text += " 公式 " + this.richTextBoxResult.Text + "執行過程:" + matlabtype.InvokeMember("Execute", BindingFlags.InvokeMethod, null, matlab, arrayInput).ToString() + "執行結束! ";

}