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

📄 matmagic.cs

📁 C#调用Matlab2008b的两个例子
💻 CS
字号:
/*
* MATLAB Compiler: 4.9 (R2008b)
* Date: Sun Mar 29 15:40:48 2009
* Arguments: "-B" "macro_default" "-W" "dotnet:MatMagic,MatMagic,0.0,private" "-d"
* "D:\MatMagic\src" "-T" "link:lib" "class{MatMagic:D:\mymagic.m}" 
*/

using System;
using System.Reflection;
using System.IO;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using MathWorks.MATLAB.NET.ComponentData;
#if SHARED
[assembly: System.Reflection.AssemblyKeyFile(@"")]
#endif

namespace MatMagic
{
  /// <summary>
  /// The MatMagic class provides a CLS compliant, MWArray interface to the M-functions
  /// contained in the files:
  /// <newpara></newpara>
  /// D:\mymagic.m
  /// <newpara></newpara>
  /// deployprint.m
  /// <newpara></newpara>
  /// printdlg.m
  /// </summary>
  /// <remarks>
  /// @Version 0.0
  /// </remarks>
  public class MatMagic : IDisposable
  {
      #region Constructors

      /// <summary internal= "true">
      /// The static constructor instantiates and initializes the MATLAB Component
      /// Runtime instance.
      /// </summary>
      static MatMagic()
      {
          if (MWArray.MCRAppInitialized)
          {
              Assembly assembly= Assembly.GetExecutingAssembly();

              string ctfFilePath= assembly.Location;

              int lastDelimeter= ctfFilePath.LastIndexOf(@"\");

              ctfFilePath= ctfFilePath.Remove(lastDelimeter, (ctfFilePath.Length - lastDelimeter));

              string ctfFileName = MCRComponentState.MCC_MatMagic_name_data + ".ctf";

              Stream embeddedCtfStream = null;

              String[] resourceStrings = assembly.GetManifestResourceNames();

              foreach (String name in resourceStrings)
                {
                  if (name.Contains(ctfFileName))
                    {
                      embeddedCtfStream = assembly.GetManifestResourceStream(name);
                      break;
                    }
                }
              mcr= new MWMCR(MCRComponentState.MCC_MatMagic_name_data,
                             MCRComponentState.MCC_MatMagic_root_data,
                             MCRComponentState.MCC_MatMagic_public_data,
                             MCRComponentState.MCC_MatMagic_session_data,
                             MCRComponentState.MCC_MatMagic_matlabpath_data,
                             MCRComponentState.MCC_MatMagic_classpath_data,
                             MCRComponentState.MCC_MatMagic_libpath_data,
                             MCRComponentState.MCC_MatMagic_mcr_application_options,
                             MCRComponentState.MCC_MatMagic_mcr_runtime_options,
                             MCRComponentState.MCC_MatMagic_mcr_pref_dir,
                             MCRComponentState.MCC_MatMagic_set_warning_state,
                             ctfFilePath, embeddedCtfStream, true);
          }
          else
          {
              throw new ApplicationException("MWArray assembly could not be initialized");
          }
      }


      /// <summary>
      /// Constructs a new instance of the MatMagic class.
      /// </summary>
      public MatMagic()
      {
      }


      #endregion Constructors

      #region Finalize

      /// <summary internal= "true">
      /// Class destructor called by the CLR garbage collector.
      /// </summary>
      ~MatMagic()
      {
          Dispose(false);
      }


      /// <summary>
      /// Frees the native resources associated with this object
      /// </summary>
      public void Dispose()
      {
          Dispose(true);

          GC.SuppressFinalize(this);
      }


      /// <summary internal= "true">
      /// Internal dispose function
      /// </summary>
      protected virtual void Dispose(bool disposing)
      {
          if (!disposed)
          {
              disposed= true;

              if (disposing)
              {
                  // Free managed resources;
              }

              // Free native resources
          }
      }


      #endregion Finalize

      #region Methods

      /// <summary>
      /// Provides a single output, 0-input MWArray interface to the mymagic M-function.
      /// </summary>
      /// <remarks>
      /// </remarks>
      /// <returns>An MWArray containing the first output argument.</returns>
      ///
      public MWArray mymagic()
      {
          return mcr.EvaluateFunction("mymagic", new MWArray[]{});
      }


      /// <summary>
      /// Provides a single output, 1-input MWArray interface to the mymagic M-function.
      /// </summary>
      /// <remarks>
      /// </remarks>
      /// <param name="x">Input argument #1</param>
      /// <returns>An MWArray containing the first output argument.</returns>
      ///
      public MWArray mymagic(MWArray x)
      {
          return mcr.EvaluateFunction("mymagic", x);
      }


      /// <summary>
      /// Provides the standard 0-input MWArray interface to the mymagic M-function.
      /// </summary>
      /// <remarks>
      /// </remarks>
      /// <param name="numArgsOut">The number of output arguments to return.</param>
      /// <returns>An Array of length "numArgsOut" containing the output
      /// arguments.</returns>
      ///
      public MWArray[] mymagic(int numArgsOut)
      {
          return mcr.EvaluateFunction(numArgsOut, "mymagic", new MWArray[]{});
      }


      /// <summary>
      /// Provides the standard 1-input MWArray interface to the mymagic M-function.
      /// </summary>
      /// <remarks>
      /// </remarks>
      /// <param name="numArgsOut">The number of output arguments to return.</param>
      /// <param name="x">Input argument #1</param>
      /// <returns>An Array of length "numArgsOut" containing the output
      /// arguments.</returns>
      ///
      public MWArray[] mymagic(int numArgsOut, MWArray x)
      {
          return mcr.EvaluateFunction(numArgsOut, "mymagic", x);
      }


      /// <summary>
      /// Provides an interface for the mymagic function in which the input and output
      /// arguments are specified as an array of MWArrays.
      /// </summary>
      /// <remarks>
      /// This method will allocate and return by reference the output argument
      /// array.<newpara></newpara>
      /// </remarks>
      /// <param name="numArgsOut">The number of output arguments to return</param>
      /// <param name= "argsOut">Array of MWArray output arguments</param>
      /// <param name= "argsIn">Array of MWArray input arguments</param>
      ///
      public void mymagic(int numArgsOut, ref MWArray[] argsOut, MWArray[] argsIn)
      {
          mcr.EvaluateFunction("mymagic", numArgsOut, ref argsOut, argsIn);
      }


      /// <summary>
      /// This method will cause a MATLAB figure window to behave as a modal dialog box.
      /// The method will not return until all the figure windows associated with this
      /// component have been closed.
      /// </summary>
      /// <remarks>
      /// An application should only call this method when required to keep the
      /// MATLAB figure window from disappearing.  Other techniques, such as calling
      /// Console.ReadLine() from the application should be considered where
      /// possible.</remarks>
      ///
      public void WaitForFiguresToDie()
      {
          mcr.WaitForFiguresToDie();
      }


      
      #endregion Methods

      #region Class Members

      private static MWMCR mcr= null;

      private bool disposed= false;

      #endregion Class Members
  }
}

⌨️ 快捷键说明

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