📄 example.cs
字号:
using System;
using System.Runtime.InteropServices;
using UtilityMatlabCompilerVer4 ;
using System.IO ;
namespace MatlabCSharpExample
{
class Example
{
IntPtr ep ;
public Example()
{
ep = MatlabCSharp.engOpen(null);
}
public void CleanUp()
{
MatlabCSharp.engClose(ep);
}
[STAThread]
static void Main(string[] args)
{
Console.Write(" Using MATLAB Engine to call the MATLAB workspace ") ;
Console.Write(" to perform the particular tasks. \n" ) ;
Example obj = new Example() ;
obj.SimplePlus() ;
Console.Write("\n") ;
obj.LinearSolve() ;
Console.Write("\n") ;
obj.MatlabEnginePlot() ;
obj.CleanUp() ;
}
/* **************************** */
public void SimplePlus()
{
double db_a = 1.2 ;
double db_b = 2.5 ;
// declare mxArray variables to use
IntPtr mx_a = (IntPtr) null ;
IntPtr mx_b = (IntPtr) null ;
IntPtr mx_c = (IntPtr) null ;
// convert Cs double to mxArray
mx_a = MatlabCSharp.double2mxArray_scalarReal(db_a) ;
mx_b = MatlabCSharp.double2mxArray_scalarReal(db_b) ;
// convert name mx_a to ml_a to use in the MATLAB workspace
MatlabCSharp.engPutVariable(ep, "ml_a", mx_a) ;
MatlabCSharp.engPutVariable(ep, "ml_b", mx_b) ;
// perform a task in MATLAB workspace
MatlabCSharp.engEvalString( ep, "ml_c = ml_a + ml_b ;" ) ;
// get result in Matlab workspace then convert to mxArray
mx_c = MatlabCSharp.engGetVariable(ep, "ml_c" ) ;
// convert mxArray to Cs double
double db_c = MatlabCSharp.mxArray2double_scalarReal(mx_c) ;
// close Matlab workspace
MatlabCSharp.engEvalString(ep, "close") ;
// print out
Console.WriteLine("The result from the simple plus: {0}", db_c.ToString() ) ;
}
/* **************************** */
public void LinearSolve()
{
double[,] A = { {1.1, 5.6, 3.3}, {4.4, 12.3, 6.6} , { 7.7, 8.8, 9.9} } ;
double[] b = { 12.5, 32.2 , 45.6 } ;
/* declare mxArray variables */
IntPtr mx_A = (IntPtr) null ;
IntPtr mx_b = (IntPtr) null ;
IntPtr mx_vectorX = (IntPtr) null ;
IntPtr mx_U = (IntPtr) null ;
/* convert Cs double to mxArray */
mx_b = MatlabCSharp.double2mxArray_vectorColumnReal(b) ;
mx_A = MatlabCSharp.double2mxArray_matrixReal(A) ;
/* begin performing tasks in the Matlab workspace
Matlab workspace tasks : Solve Ax = b, and get
a vector x and an upper matrix U.
The problem Ax = b is solved here as the purpose
of showing the use of the MATLAB workspace in a C# function. */
MatlabCSharp.engPutVariable(ep, "ml_A", mx_A) ;
MatlabCSharp.engPutVariable(ep, "ml_b", mx_b) ;
MatlabCSharp.engEvalString(ep, " ml_vectorX = mldivide(ml_A, ml_b) ; " ) ;
MatlabCSharp.engEvalString(ep, " [ml_L, ml_U, ml_P] = lu( ml_A ) ; " ) ;
/* end tasks in the Matlab workspace */
/* get results in the Matlab workspace then convert to mxArray */
mx_vectorX = MatlabCSharp.engGetVariable(ep, "ml_vectorX" ) ;
mx_U = MatlabCSharp.engGetVariable(ep, "ml_U" ) ;
/* convert mxArray to Cs double */
double[] db_vectorX = MatlabCSharp.mxArray2double_vectorReal(mx_vectorX) ;
double[,] db_matrixU = MatlabCSharp.mxArray2double_matrixReal(mx_U) ;
/* close Matlab workspace */
MatlabCSharp.engEvalString(ep, "close") ;
/* print out */
Console.WriteLine("Solution of the equation Ax = b is: " ) ;
MatlabCSharp.printVector(db_vectorX) ;
Console.WriteLine("The upper matrix U :" ) ;
MatlabCSharp.printMatrix(db_matrixU) ;
}
/* *************************** */
public void MatlabEnginePlot()
{
double[] X = { 12.5, 32.2 , 45.6 } ;
double[] Y = { 12.5, 32.2 , 45.6 } ;
/* declare mxArray variables */
IntPtr mx_X = (IntPtr) null ;
IntPtr mx_Y = (IntPtr) null ;
// Convert Cs double to mxArray
mx_X = MatlabCSharp.double2mxArray_vectorColumnReal(X) ;
mx_Y = MatlabCSharp.double2mxArray_vectorColumnReal(Y) ;
/* transfer name mx_X to the name, ml_X */
MatlabCSharp.engPutVariable(ep, "ml_X" , mx_X ) ;
MatlabCSharp.engPutVariable(ep, "ml_Y" , mx_Y ) ;
/* perform tasks in the Matlab workspace */
MatlabCSharp.engEvalString(ep, " plot(ml_X, ml_Y ,'r'); " ) ;
/* keep the figure */
Console.WriteLine("Hit Enter key to close the figure and continue") ;
System.Console.Read() ;
/* close the Matlab workspace */
MatlabCSharp.engEvalString(ep, "close") ;
}
} // end class
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -