ဥပမာ (၁)
ဥပမာ (၂) Computing the integral -->
ဥပမာ (၃)
C# ကေန MatLab ကို လွမ္းေခၚအသံုးျပဳခ်င္တယ္ဆိုရင္ C# Project ထဲက Reference မွာ Matlab Application ကို Add ထားဖို႔ေတာ့လိုပါတယ္ ..
(right click References --> Add Reference.. --> COM ထဲမွာ Matlab Application ဆိုတာ ရွိပါတယ္)
Source Code : Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CallingMatLabFromCSharp
{
public partial class Form1 : Form
{
private MLApp.MLAppClass MatLab;
public Form1()
{
InitializeComponent();
MatLab = new MLApp.MLAppClass();
}
private void button1_Click(object sender, EventArgs e)
{
this.txtResult.Clear();
this.txtResult.AppendText(MatLab.Execute(this.txtMatLabCommand.Text).Replace("\n", Environment.NewLine));
}
private void button2_Click(object sender, EventArgs e)
{
this.txtMatLabCommand.Clear();
this.txtResult.Clear();
}
}
}
Source Code : Form1.Designer.cs
namespace CallingMatLabFromCSharp
{
partial class Form1
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.txtMatLabCommand = new System.Windows.Forms.RichTextBox();
this.txtResult = new System.Windows.Forms.RichTextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox6 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.groupBox5 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.groupBox6.SuspendLayout();
this.groupBox3.SuspendLayout();
this.groupBox5.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// txtMatLabCommand
//
this.txtMatLabCommand.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txtMatLabCommand.ForeColor = System.Drawing.Color.Blue;
this.txtMatLabCommand.Location = new System.Drawing.Point(10, 19);
this.txtMatLabCommand.Name = "txtMatLabCommand";
this.txtMatLabCommand.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;
this.txtMatLabCommand.Size = new System.Drawing.Size(273, 125);
this.txtMatLabCommand.TabIndex = 0;
this.txtMatLabCommand.Text = "";
//
// txtResult
//
this.txtResult.BackColor = System.Drawing.SystemColors.ControlText;
this.txtResult.ForeColor = System.Drawing.Color.Lime;
this.txtResult.Location = new System.Drawing.Point(10, 18);
this.txtResult.Name = "txtResult";
this.txtResult.Size = new System.Drawing.Size(273, 163);
this.txtResult.TabIndex = 1;
this.txtResult.Text = "";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.groupBox6);
this.groupBox1.Controls.Add(this.groupBox3);
this.groupBox1.Controls.Add(this.groupBox5);
this.groupBox1.Controls.Add(this.groupBox2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(5, 4);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(308, 422);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
//
// groupBox6
//
this.groupBox6.Controls.Add(this.button1);
this.groupBox6.Location = new System.Drawing.Point(138, 170);
this.groupBox6.Name = "groupBox6";
this.groupBox6.Size = new System.Drawing.Size(135, 44);
this.groupBox6.TabIndex = 4;
this.groupBox6.TabStop = false;
//
// button1
//
this.button1.ForeColor = System.Drawing.Color.Blue;
this.button1.Location = new System.Drawing.Point(6, 9);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(123, 31);
this.button1.TabIndex = 0;
this.button1.Text = "Execute";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.txtResult);
this.groupBox3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.groupBox3.ForeColor = System.Drawing.Color.Red;
this.groupBox3.Location = new System.Drawing.Point(6, 224);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(294, 191);
this.groupBox3.TabIndex = 1;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Result";
//
// groupBox5
//
this.groupBox5.Controls.Add(this.button2);
this.groupBox5.Location = new System.Drawing.Point(43, 171);
this.groupBox5.Name = "groupBox5";
this.groupBox5.Size = new System.Drawing.Size(76, 44);
this.groupBox5.TabIndex = 3;
this.groupBox5.TabStop = false;
//
// button2
//
this.button2.ForeColor = System.Drawing.Color.Red;
this.button2.Location = new System.Drawing.Point(6, 9);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(63, 31);
this.button2.TabIndex = 1;
this.button2.Text = "clc";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.txtMatLabCommand);
this.groupBox2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.groupBox2.ForeColor = System.Drawing.Color.Red;
this.groupBox2.Location = new System.Drawing.Point(7, 11);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(294, 154);
this.groupBox2.TabIndex = 0;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "MATLAB Command Window";
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(7, 166);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(294, 56);
this.label1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(317, 429);
this.Controls.Add(this.groupBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Calling MatLab From C#";
this.groupBox1.ResumeLayout(false);
this.groupBox6.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.groupBox5.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.RichTextBox txtMatLabCommand;
private System.Windows.Forms.RichTextBox txtResult;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.GroupBox groupBox6;
private System.Windows.Forms.GroupBox groupBox5;
private System.Windows.Forms.Label label1;
}
}
Download :
exe
https://sites.google.com/site/files4ogresite/CallingMatLabFromCSharp_EXE.rar
Source Code
https://sites.google.com/site/files4ogresite/CallingMatLabFromCSharp_SourceCode.rar