?? program.cs
字號:
using System;
using System.Collections.Generic;
using System.Text;
// MathWorks assemblies that ship with Builder for .NET
// and should be registered in Global Assembly Cache
using MathWorks.MATLAB.NET.Utility;
using MathWorks.MATLAB.NET.Arrays;
// Assembly built by Builder for .NET containing
// math_on_numbers.m
using dotnet;
// To include the C Shared Library created with
// MATLAB Compiler we need to include InteropServices
// to integrate the unmanaged code
using System.Runtime.InteropServices;
// Copyright 2006, 2007 The MathWorks, Inc.
namespace example_ML_integration
{
class Program
{
static void Main(string[] args)
{
//////////////////////
// Runtime Parameters
//////////////////////
// Create a mode variable to determine which
// method is used.
// 1 = MATLAB Com engine interface
// 2 = .NET Assembly
// 3 = C shared library
System.Int16 Mode = 3;
////////////////////
// Input Parameters
////////////////////
// create an array ar for the real part of "a"
System.Array ar = new double[2];
ar.SetValue(11, 0);
ar.SetValue(12, 1);
// create an array ai for the imaginary part of "a"
System.Array ai = new double[2];
ai.SetValue(1, 0);
ai.SetValue(2, 1);
// create an array br for the real part of "b"
System.Array br = new double[2];
br.SetValue(21, 0);
br.SetValue(22, 1);
// create an array bi for the imaginary part of "b"
System.Array bi = new double[2];
bi.SetValue(3, 0);
bi.SetValue(4, 1);
/////////////////////
// Output Parameters
/////////////////////
// initialize variables for return value from ML
System.Array cr = new double[2];
System.Array ci = new double[2];
System.Array dr = new double[2];
System.Array di = new double[2];
////////////////////////
// Call MATLAB function
////////////////////////
// call appropriate function/method based on Mode
if (Mode == 1)
{
// use MATLAB engine
UseEngine(ar, ai, br, bi, ref cr, ref ci, ref dr, ref di);
}
else if (Mode == 2)
{
// use .NET assembly created with Builder for .NET
UseDotNET(ar, ai, br, bi, ref cr, ref ci, ref dr, ref di);
}
else if (Mode == 3)
{
// use C Shared Library created with MATLAB Compiler
UseClib(ar, ai, br, bi, ref cr, ref ci, ref dr, ref di);
}
// any other value of Mode is invalid and we can do nothing
/////////////////////
// Output to console
/////////////////////
DisplayMethod(Mode);
DisplayArgs( true, ar, ai, br, bi); // true = input
DisplayArgs(false, cr, ci, dr, di); // false = not-input or output
DisplayEnd();
}
/*
* All input and output arguments must have been allocated
* prior to calling these functions
*
* Input:
* ar real part of a
* ai imaginary part of a
* br real part of b
* bi imaginary part of b
*
* Output:
* cr real part of c
* ci imaginary part of c
* dr real part of d
* di imaginary part of d
*/
static private void UseEngine(Array ar, Array ai, Array br,
Array bi, ref Array cr, ref Array ci, ref Array dr, ref Array di)
{
/*
* This function calls the math_by_numbers routine inside
* MATLAB using the MATLAB Engine's com interface
*/
// Instantiate MATLAB Engine Interface through com
MLApp.MLAppClass matlab = new MLApp.MLAppClass();
// Using Engine Interface, put the matrix "a" into
// the base workspace.
// "a" is a complex variable with a real part of ar,
// and an imaginary part of ai
matlab.PutFullMatrix("a", "base", ar, ai);
// Using Engine Interface, put the matrix "b" into
// the base workspace.
// "b" is a complex variable with a real part of br,
// and an imaginary part of bi
matlab.PutFullMatrix("b", "base", br, bi);
// Using Engine Interface, execute the ML command
// contained in quotes.
matlab.Execute("cd c:\\demos\\CSharp_MATLAB\\mcode;");
matlab.Execute("open math_on_numbers.m");
matlab.Execute("dbstop in math_on_numbers.m");
matlab.Execute("[c, d] = math_on_numbers(a,b);");
matlab.Execute("com.mathworks.mlservices.MLEditorServices.closeAll");
//matlab.Execute("dbquit all");
// Using Engine Interface, get the matrix "c" from
// the base workspace.
// "c" is a complex variable with a real part of cr,
// and an imaginary part of ci
matlab.GetFullMatrix("c", "base", ref cr, ref ci);
// using engine interface, get the matrix "c" from
// the base workspace.
// "d" is a complex variable with a real part of dr,
// and an imaginary part of di
matlab.GetFullMatrix("d", "base", ref dr, ref di);
}
static private void UseDotNET(Array ar, Array ai, Array br,
Array bi, ref Array cr, ref Array ci, ref Array dr, ref Array di)
{
/*
* This function calls the math_by_numbers method from
* inside a .NET assembly created with MATLAB using Builder
* for .NET.
*/
// Instantiate our .NET class from the MATLAB created component
dotnetclass AClass = new dotnetclass();
// explicity convert our input arguments into MWArrays
// this can be done with implicit conversion
MWNumericArray a = new MWNumericArray(1, 2, (double[])ar, (double[])ai);
MWNumericArray b = new MWNumericArray(1, 2, (double[])br, (double[])bi);
// call math_on_method from Assembly specifying the number
// of return arguments expected and passing in a and b
MWArray[] RetVal = AClass.math_on_numbers(2, a, b);
// Unpack return values seperating the real and imaginary parts
// using the ToArray method of MWNummericArray. Since RetVal was
// declared as a MWArray above, it must be explicity typecast here.
cr = ((MWNumericArray) RetVal[0]).ToVector(MWArrayComponent.Real);
ci = ((MWNumericArray) RetVal[0]).ToVector(MWArrayComponent.Imaginary);
dr = ((MWNumericArray) RetVal[1]).ToVector(MWArrayComponent.Real);
di = ((MWNumericArray) RetVal[1]).ToVector(MWArrayComponent.Imaginary);
}
static private void UseClib(Array ar, Array ai, Array br,
Array bi, ref Array cr, ref Array ci, ref Array dr, ref Array di)
{
/*
* This function calls the math_by_numbers method from
* inside a C Shared Library created with MATLAB Compiler.
*/
// A new class Clib is used to wrap the unmanaged library.
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcreatingprototypesinmanagedcode.asp
// The new class is instantiated here.
Clib AClass = new Clib();
AClass.DoTheWork(ar, ai, br, bi, ref cr, ref ci, ref dr, ref di);
}
class Clib
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -