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

📄 program.cs

📁 matlab 与 C# 互编程.proficient in C# for the comrades, simulation of very complex procedures, if they ca
💻 CS
📖 第 1 页 / 共 2 页
字号:
        {
            // Wrap all of the unmanaged functions that need to
            // be called directly using DLLImport
            // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconconsumingunmanageddllfunctions.asp
            [DllImport("cshared.dll")]
            private static extern void mlfMath_on_numbers([In]Int32 nargout, ref IntPtr c, ref IntPtr d, [In]IntPtr a, [In]IntPtr b);

            [DllImport("cshared.dll")]
            private static extern void csharedInitialize();

            [DllImport("cshared.dll")]
            private static extern void csharedTerminate();

            [DllImport("mclmcrrt77.dll")]
            private static extern IntPtr mxCreateDoubleMatrix_700([In]Int32 numRows, [In]Int32 numCols, [In]Int32 mxComplexity);
            /*
             * typedef enum {
             *     mxREAL,
             *     mxCOMPLEX
             * } mxComplexity;
             */

            [DllImport("mclmcrrt77.dll")]
            private static extern IntPtr mxGetPr([In] IntPtr mxArray);

            [DllImport("mclmcrrt77.dll")]
            private static extern IntPtr mxGetPi([In] IntPtr mxArray);

            [DllImport("mclmcrrt77.dll")]
            private static extern void mxDestroyArray([In] IntPtr mxArray);

            [DllImport("mclmcrrt77.dll")]
            private static extern bool mclInitializeApplication(string options, Int32 count);

            [DllImport("mclmcrrt77.dll")]
            private static extern void mclTerminateApplication();

            public void DoTheWork(Array ar, Array ai, Array br, Array bi,
                ref Array cr, ref Array ci, ref Array dr, ref Array di)
            {
                //////////////////
                // Initialization
                //////////////////
                
                // Initialize MCR
                bool RetVal;
                RetVal = mclInitializeApplication("NULL",0);

                // Initalize our csharedlib
                csharedInitialize();

                //////////////////////////
                // Create Input Arguments
                //////////////////////////
                // create an mxarray to contain a and b
                IntPtr a_ptr = mxCreateDoubleMatrix_700(1, 2, 1);  /* 1 row, 2 cols, IsComplex */
                IntPtr b_ptr = mxCreateDoubleMatrix_700(1, 2, 1);  /* 1 row, 2 cols, IsComplex */
                
                // get pointer to real and imaginary parts of a
                IntPtr ar_ptr = mxGetPr(a_ptr);
                IntPtr ai_ptr = mxGetPi(a_ptr);
                
                // get pointer to real and imaginary parts of b
                IntPtr br_ptr = mxGetPr(b_ptr);
                IntPtr bi_ptr = mxGetPi(b_ptr);
                
                
                ////////////////
                // Copy In Data
                ////////////////
                
                // copy values to unmanaged array (mwArray)
                // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemruntimeinteropservicesmarshalclasswritebytetopic2.asp
                Marshal.Copy((double[])ar, 0, ar_ptr, 2);
                Marshal.Copy((double[])ai, 0, ai_ptr, 2);
                Marshal.Copy((double[])br, 0, br_ptr, 2);
                Marshal.Copy((double[])bi, 0, bi_ptr, 2);
                

                ///////////////////////////
                // Create Output Arguments
                ///////////////////////////
                
                // create pointers for return values c and d
                IntPtr c_ptr = IntPtr.Zero;
                IntPtr d_ptr = IntPtr.Zero;


                ////////////////////////////////
                // Call function from C library
                ////////////////////////////////
                
                // call math on numbers
                try
                {
                    mlfMath_on_numbers(2, ref c_ptr, ref d_ptr, a_ptr, b_ptr);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

                /////////////////
                // Copy Out Data
                /////////////////
                
                // get pointer to real and imaginary parts of a
                IntPtr cr_ptr = mxGetPr(c_ptr);
                IntPtr ci_ptr = mxGetPi(c_ptr);

                // get pointer to real and imaginary parts of b
                IntPtr dr_ptr = mxGetPr(d_ptr);
                IntPtr di_ptr = mxGetPi(d_ptr);

                // copy values from unmanaged array (mwArray)
                Marshal.Copy(cr_ptr, (double[])cr, 0, 2);
                Marshal.Copy(ci_ptr, (double[])ci, 0, 2);
                Marshal.Copy(dr_ptr, (double[])dr, 0, 2);
                Marshal.Copy(di_ptr, (double[])di, 0, 2);

                
                ///////////
                // CleanUp
                ///////////
                
                // destroy mxarrays
                mxDestroyArray(a_ptr);
                mxDestroyArray(b_ptr);

                // Terminate libraries
                Clib.csharedTerminate();
                Clib.mclTerminateApplication();
            
            } // end function DoTheWork()

        } // end class Clib

        static private void DisplayMethod([In]Int16 Mode)
        {
            String Part1 = "Calling MATLAB code via ";
            String Part2 = String.Empty;
            String Empty = "";

            switch (Mode)
            {
                case 1: Part2 = Part2 + "Engine Interface."; break;
                case 2: Part2 = Part2 + ".NET Assembly."   ; break;
                case 3: Part2 = Part2 + "C Shared Library."; break;
            }

            Console.WriteLine(Empty);

            Console.WriteLine(String.Concat(Part1, Part2));

        } // end function DisplayMethod

        static private void DisplayArgs(Boolean In, [In]Array OneReal, [In]Array OneImag,
            [In]Array TwoReal, [In]Array TwoImag)
        {
            // declare variables to hold output
            String [] Labels = new String[2];
            String []   Line = new String[6];
            
            // allow for a space between each set of variables
            Line[3] = "";
            
            // setup first output line and labels
            // based on whether this is an input
            // or an output
            if (In)
            {
                // this is the first output, so
                // prepare the console
                Console.Clear();

                Line[0] = "Using input args:";
                Labels[0] = "a";
                Labels[1] = "b";
            }
            else
            {
                Line[0] = "Return Values are:";
                Labels[0] = "c";
                Labels[1] = "d";
            }
            
            // format the variable output lines
            for (int element = 0; element < 2; element++)
            {
                Line[1 + element] = String.Format("{0}({1}) = {2,5:.####} + {3,5:.####}*i", Labels[0], (element + 1), OneReal.GetValue(element), OneImag.GetValue(element));
                Line[4 + element] = String.Format("{0}({1}) = {2,5:.####} + {3,5:.####}*i", Labels[1], (element + 1), TwoReal.GetValue(element), TwoImag.GetValue(element));

            }

            // write output to screen
            Console.WriteLine("");

            for (int i = 0; i < 6; i++)
            {
                Console.WriteLine(Line[i]);
            }
            
        } // end function DisplayArgs

        static private void DisplayEnd()
        {
            ConsoleKeyInfo keypressed;

            Console.WriteLine();
            Console.WriteLine("Press any key to exit.");

            keypressed = Console.ReadKey(true);

        } // end function DisplayEnd

    } // end class Program

} // end namespace example_ML_integration

⌨️ 快捷键说明

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