⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csharpmatlabcompilerver4.cs

📁 MATLAB C# Book. This book is a great tutorial for C# programmers who use MATLAB to develop applicati
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Runtime.InteropServices;
namespace UtilityMatlabCompilerVer4
{

	class MatlabCSharp  // for MATLAB 7 and MATLAB Compiler 4
	{

		[ DllImport( "libmx.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern IntPtr mxCreateDoubleMatrix(int m, int n, mxComplexity flag);

		public enum mxComplexity
		{ mxREAL, mxCOMPLEX };

		[ DllImport( "libmx.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern void mxSetPr( IntPtr pa, IntPtr preal	);

		[ DllImport( "libmx.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern void mxSetPi( IntPtr pa, IntPtr pimag	);


		[ DllImport( "libmx.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern int mxGetM(IntPtr pa);	

		[ DllImport( "libmx.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern void mxSetM( IntPtr pa, int m );

		[ DllImport( "libmx.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern int mxGetN( IntPtr pa	);

		[ DllImport( "libmx.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern void mxSetN(IntPtr pa, int n);

		[ DllImport( "libmx.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern IntPtr mxGetPr(IntPtr pa);  

		[ DllImport( "libmx.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern IntPtr mxGetPi(IntPtr pa);  

		[ DllImport( "libmx.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern IntPtr mxDuplicateArray(IntPtr pa);


		[ DllImport( "libmx.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern IntPtr mxCreateString(String str);


	// DLL for MATLAB Engine
		[ DllImport( "libeng.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern IntPtr engOpen(string startMATLAB);
	
		[ DllImport( "libeng.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern int engClose(IntPtr ep);

		[ DllImport( "libeng.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern int engPutVariable(IntPtr ep, string ML_name, IntPtr mx_name);

		[ DllImport( "libeng.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern int engEvalString(IntPtr ep, string ML_command);

		[ DllImport( "libeng.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern IntPtr engGetVariable(IntPtr ep, string ML_resultName);			

	// DLL for MATLAB Engine -- End

	// SCALAR
		public static IntPtr double2mxArray_scalarReal(double dbScalar)
		{
			// convert a double scalar to an mxArray mxPointer 

			double[] dbBuffer ;
			dbBuffer = new double[1];

			IntPtr mxPointer		= (IntPtr)0	;
			IntPtr mxPointerBuffer	= (IntPtr)0	;
			IntPtr mxPointerTemp ;
			
			try
			{
				mxPointerBuffer = mxCreateDoubleMatrix(1, 1, MatlabCSharp.mxComplexity.mxREAL);
			}
			catch 
			{
				System.Console.WriteLine("We cannot create a pointer of mxArray. Please check again." ) ;
				return (IntPtr)0;
			}
		
				
			dbBuffer[0] = dbScalar;
				
			mxPointerTemp = Marshal.AllocHGlobal(dbBuffer.Length * Marshal.SizeOf(dbBuffer[0]));
			Marshal.Copy(dbBuffer, 0, mxPointerTemp, 1);
			mxSetPr(mxPointerBuffer, mxPointerTemp);

			mxPointer = mxDuplicateArray(mxPointerBuffer);
			Marshal.FreeHGlobal(mxPointerTemp);

			return mxPointer ;
		}


		public static IntPtr double2mxArray_scalarComplex(double dbScalarReal, double dbScalarImag)
		{
			// convert double scalars to a complex mxArray mxPointer 

			double[] dbBufferReal ;
			dbBufferReal = new double[1];

			double[] dbBufferImag ;
			dbBufferImag = new double[1];

			IntPtr mxPointer		= (IntPtr)0	;
			IntPtr mxPointerBuffer	= (IntPtr)0	;

			IntPtr mxPointerTempReal ;
			IntPtr mxPointerTempImag ;
			
			try
			{
				  mxPointerBuffer = mxCreateDoubleMatrix(1, 1, MatlabCSharp.mxComplexity.mxCOMPLEX);
			}
			catch 
			{
				System.Console.WriteLine("We cannot create a pointer of mxArray. Please check again." ) ;
				return (IntPtr)0;
			}
		
				
			dbBufferReal[0] = dbScalarReal;
			dbBufferImag[0] = dbScalarImag;
				
			mxPointerTempReal = Marshal.AllocHGlobal(dbBufferReal.Length * Marshal.SizeOf(dbBufferReal[0]));
			mxPointerTempImag = Marshal.AllocHGlobal(dbBufferImag.Length * Marshal.SizeOf(dbBufferImag[0]));


			Marshal.Copy(dbBufferReal, 0, mxPointerTempReal, 1);
			Marshal.Copy(dbBufferImag, 0, mxPointerTempImag, 1);

			mxSetPr(mxPointerBuffer, mxPointerTempReal);
			mxSetPi(mxPointerBuffer, mxPointerTempImag);


			mxPointer = mxDuplicateArray(mxPointerBuffer);

			Marshal.FreeHGlobal(mxPointerTempReal);
			Marshal.FreeHGlobal(mxPointerTempImag);

			return mxPointer ;
		}


		public static double mxArray2double_scalarReal(IntPtr mxPointer)
		{
			// convert a real term of an mxArray mxPointer to a double scalar

			double dbScalar ;

			IntPtr PointerRealScalar;
			double [] dbVector ;
			dbVector = new double[1] ;
			PointerRealScalar = mxGetPr(mxPointer);

			Marshal.Copy(PointerRealScalar, dbVector, 0, 1);
			dbScalar = dbVector[0] ;

			return dbScalar;

		}


		public static void mxArray2double_scalarComplex(IntPtr mxPointer, ref double dbReal, ref double dbImag )
		{
			// convert a complex mxArray mxPointer to double scalars

			IntPtr PointerScalarReal;
			IntPtr PointerScalarImag = (IntPtr)null;

			// for real term
			double [] dbVectorReal ;
			dbVectorReal = new double[1] ;
			PointerScalarReal = mxGetPr(mxPointer);
			Marshal.Copy(PointerScalarReal, dbVectorReal, 0, 1);
			dbReal = dbVectorReal[0] ;


			// for imaginary term
			double [] dbVectorImag ;
			dbVectorImag = new double[1] ;

			try
			{ 

				PointerScalarImag = mxGetPi(mxPointer);
				Marshal.Copy(PointerScalarImag, dbVectorImag, 0, 1);
				dbImag = dbVectorImag[0] ;
			}

			catch 
			{
				dbImag = 0.0 ;
			}


		}

		
	// VECTOR
		public static IntPtr double2mxArray_vectorColumnReal(double[] dbVector)
		{				
			// convert a double vector to an mxArray mxPointer 
			// transfer to an mxArray vector with number of col = 1

			int vectorSize ;	
			vectorSize = dbVector.GetUpperBound(0)- dbVector.GetLowerBound(0) + 1;

			IntPtr mxPointer		= (IntPtr)0;
			IntPtr mxPointerBuffer	= (IntPtr)0;
			IntPtr mxPointerTemp ;

			int row = vectorSize ;
			double[] dbVectorBuffer;
			dbVectorBuffer = new double[row];
			
			try
			{
				mxPointerBuffer = mxCreateDoubleMatrix(row, 1, MatlabCSharp.mxComplexity.mxREAL);
			}
			catch 
			{
				System.Console.Write("We cannot create a pointer mxArray. Please check again." ) ;
				return (IntPtr)0;
			}


			mxPointerTemp = Marshal.AllocHGlobal(dbVector.Length * Marshal.SizeOf(dbVector[0]));

			Marshal.Copy(dbVector, 0, mxPointerTemp, dbVector.Length);
			mxSetPr(mxPointerBuffer, mxPointerTemp);

			mxPointer = mxDuplicateArray(mxPointerBuffer);
			Marshal.FreeHGlobal(mxPointerTemp);

			return mxPointer;	
	
		}

		public static IntPtr double2mxArray_vectorRowReal(double[] dbVector)
		{				
			// convert a double vector to an mxArray mxPointer 
			// transfer to an mxArray vector with number of row = 1

			int vectorSize ;	
			vectorSize = dbVector.GetUpperBound(0)- dbVector.GetLowerBound(0) + 1;

			IntPtr mxPointer		= (IntPtr)0;
			IntPtr mxPointerBuffer	= (IntPtr)0;
			IntPtr mxPointerTemp ;

			int col = vectorSize ;
			double[] dbVectorBuffer;
			dbVectorBuffer = new double[col];
			
			try
			{
				mxPointerBuffer = mxCreateDoubleMatrix(1, col, MatlabCSharp.mxComplexity.mxREAL);
			}
			catch 
			{
				System.Console.Write("We cannot create a pointer mxArray. Please check again." ) ;
				return (IntPtr)0;
			}


			mxPointerTemp = Marshal.AllocHGlobal(dbVector.Length * Marshal.SizeOf(dbVector[0]));

			Marshal.Copy(dbVector, 0, mxPointerTemp, dbVector.Length);
			mxSetPr(mxPointerBuffer, mxPointerTemp);

			mxPointer = mxDuplicateArray(mxPointerBuffer);
			Marshal.FreeHGlobal(mxPointerTemp);

			return mxPointer;	
	
		}


		public static IntPtr double2mxArray_vectorColumnComplex(double[] dbVectorReal, double[] dbVectorImag)
		{				
			// convert double vectors to a complex mxArray mxPointer 
			// transfer to an mxArray vector with number of col = 1

			int vectorSize ;	
			vectorSize = dbVectorReal.GetUpperBound(0)- dbVectorReal.GetLowerBound(0) + 1;

			IntPtr mxPointer		= (IntPtr)0;
			IntPtr mxPointerBuffer	= (IntPtr)0;

			IntPtr mxPointerTempReal ;
			IntPtr mxPointerTempImag ;

			int row = vectorSize ;
			double[] dbVectorBuffer;
			dbVectorBuffer = new double[row];
			
			try
			{
				mxPointerBuffer = mxCreateDoubleMatrix(row, 1, MatlabCSharp.mxComplexity.mxCOMPLEX);
			}
			catch 
			{
				System.Console.Write("We cannot create a pointer mxArray. Please check again." ) ;
				return (IntPtr)0;
			}


			mxPointerTempReal = Marshal.AllocHGlobal(dbVectorReal.Length * Marshal.SizeOf(dbVectorReal[0]));
			mxPointerTempImag = Marshal.AllocHGlobal(dbVectorImag.Length * Marshal.SizeOf(dbVectorImag[0]));

			Marshal.Copy(dbVectorReal, 0, mxPointerTempReal, dbVectorReal.Length);
			Marshal.Copy(dbVectorImag, 0, mxPointerTempImag, dbVectorImag.Length);

			mxSetPr(mxPointerBuffer, mxPointerTempReal);
			mxSetPi(mxPointerBuffer, mxPointerTempImag);

			mxPointer = mxDuplicateArray(mxPointerBuffer);

			Marshal.FreeHGlobal(mxPointerTempReal);
			Marshal.FreeHGlobal(mxPointerTempImag);

			return mxPointer;	
	
		}

		public static IntPtr double2mxArray_vectorRowComplex(double[] dbVectorReal, double[] dbVectorImag)
		{				
			// convert double vectors to a complex mxArray mxPointer 
			// transfer to an mxArray vector with number of row = 1

			int vectorSize ;	
			vectorSize = dbVectorReal.GetUpperBound(0)- dbVectorReal.GetLowerBound(0) + 1;

			IntPtr mxPointer		= (IntPtr)0;
			IntPtr mxPointerBuffer	= (IntPtr)0;

			IntPtr mxPointerTempReal ;
			IntPtr mxPointerTempImag ;

			int col = vectorSize ;
			double[] dbVectorBuffer;
			dbVectorBuffer = new double[col];
			
			try
			{
				mxPointerBuffer = mxCreateDoubleMatrix(1, col, MatlabCSharp.mxComplexity.mxCOMPLEX);

⌨️ 快捷键说明

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