example.cs

来自「MATLAB C# Book. This book is a great tut」· CS 代码 · 共 71 行

CS
71
字号
using System;
using System.Runtime.InteropServices;
using UtilityMatlabCompilerVer4 ;

namespace MatlabCSharpExample
{
	class Example
	{
		//constructor
		public Example()
		{
			mypluslibInitialize();	
		}

		public void CleanUp()
		{
			mypluslibTerminate();	
		}

/* declare dll functions */
		[ DllImport( "mypluslib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mypluslibInitialize(); 

		[ DllImport( "mypluslib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mypluslibTerminate(); 

		[ DllImport( "mypluslib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMyplus(int nargout, ref IntPtr y, IntPtr a, IntPtr b);


/* end dll functions */

		[STAThread]
		static void Main(string[] args)
		{
			Console.WriteLine(" ") ;

			Example obj = new Example() ; 
			
			Console.WriteLine("Generating a C Shared Library from MATLAB M-Files " ) ;
			Console.Write("\n") ;

			double c = obj.CalculatePlus(2.1, 3.4) ;
			Console.WriteLine(c.ToString() ) ;

			obj.CleanUp() ;
		}

		public double CalculatePlus(double a, double b) 
		{
			/* declare mxArray variables */
			IntPtr mx_a = (IntPtr) null ;
			IntPtr mx_b = (IntPtr) null ;
			IntPtr mx_y = (IntPtr) null ;

			/* convert Cs double to mxArray */
			mx_a = MatlabCSharp.double2mxArray_scalarReal(a) ;
			mx_b = MatlabCSharp.double2mxArray_scalarReal(b) ;

			/* call the implemental function */
			mlfMyplus(1, ref mx_y, mx_a, mx_b);

			/* convert back mxArray to Cs double */
			double result = MatlabCSharp.mxArray2double_scalarReal(mx_y) ;

			return result ;
		}

	} // end class 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?