目标:C#调用Matlab生成的dll文件(动态链接库);以matlab的fft函数为例,C#内生成两列数组,作为输入复数数组的实部和虚部,对该复数数组进行一维傅里叶变换,命令行输出变换后的函数实部和虚部。

第一步:Matlab编写函数通过deployTool生成C#可以调用的dll文件

Matlab创建新文档命名为myfft.m,键入如下代码并保存:

% 对输入数据进行快速傅里叶变换
function [BReal,BImag] = myfft(AReal,AImag)
AComplex = complex(AReal,AImag);
nfft     = 2^nextpow2(length(AReal));
BComplex = fft(AComplex,nfft);
BReal    = real(BComplex);
BImag    = imag(BComplex);
end

 然后,在matlab的命令行窗口输入deploytool,进入Compiler选择页面,选择Library Compiler,进入后选择.NET Assembly,添加刚刚编写的myfft.m

C#调用Matlab2018 dll文件

C#调用Matlab2018 dll文件

修改下类名称

C#调用Matlab2018 dll文件

单击右上角的Package按钮,完成dll文件打包。

 

第二步:在C#工程内调用Matlab刚刚生成的dll文件。

新建控制台C#工程,命名为CsharpMatlabFFT,并设置为x64位,与matlab版本相同,matlab版本为32位应选择C#项目为32位。

C#调用Matlab2018 dll文件

添加引用,MWArray.dll,64位请添加红框路径内的dll文件

C#调用Matlab2018 dll文件

添加引用,myfftNative.dll文件,在刚刚matlab生成的for_redistribution_files_only路径下

C#调用Matlab2018 dll文件

在新建的C#项目下找到program.cs文件,文件头添加代码块;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using myfftNative;
using MathWorks.MATLAB.NET.Arrays; //添加引用
using MathWorks.MATLAB.NET.Utility;//添加引用

namespace CsharpMatlabFFT
{
    class Program
    {
        static void Main(string[] args)
        {
            /**初始化输入变量*/
            int[] AReal = new int[1000];
            int[] AImag = new int[1000];

            /**循环赋值*/
            for (int i = 0; i<AReal.Length; i++)
            {
                AReal[i] = 1;
                AImag[i] = 0;
            }

            /**转化为Matlab输入变量*/
            MWArray MWAReal = new MWNumericArray(AReal);
            MWArray MWAImag = new MWNumericArray(AImag);

            /**实例化Matlab自建类ClassMyFFT对象*/
            myfftNative.ClassMyFFT fft = new ClassMyFFT();

            /**调用Matlab自建类ClassMyFFT对象内myfft方法*/
            object resultObj = fft.myfft(2, AReal, AImag);    //2为返回参数个数,AReal及AImag为输入参数
            object[] resultObjs = (object[])resultObj;
            
            /**将matlab的类型转换为C#的数据类型*/
            double[,] BReal = (double[,])resultObjs[0];
            double[,] BImag = (double[,])resultObjs[1];

            /**控制台输出*/
            for (int i = 0; i < AReal.Length; i++)
            {
                Console.WriteLine(Convert.ToString(i) + " " + Convert.ToString(BReal[0,i])+" "+Convert.ToString(BImag[0, i]));
            }
        }
    }
}

异常排除:

遇到类似如下情况,表示当前计算机内未安装对应版本的Matlab MCR_Installer,在Matlab官网上下载后安装并设置环境变量后即可运行,设置环境变量的方法参考https://jingyan.baidu.com/article/19020a0a057462529d2842d6.html

未经处理的异常:  System.Exception: MWArray assembly failed to be initialized ---> System.Exception: The MATLAB Runtime instance could not be initialized ---> System.ApplicationException: The MATLAB Runtime instance could not be initialized
Error initializing MATLAB Runtime: Error in initializing CTF core properties. Details: 'BadCast'
   在 MathWorks.MATLAB.NET.Utility.MWMCR..ctor(String componentName, String componentPath, Boolean isLibrary)
   --- 内部异常堆栈跟踪的结尾 ---
   在 MathWorks.MATLAB.NET.Utility.MWMCR..ctor(String componentName, String componentPath, Boolean isLibrary)
   在 myfftNative.Cmyfft..cctor()
   --- 内部异常堆栈跟踪的结尾 ---
   在 myfftNative.Cmyfft..ctor()
   在 CsharpMatlabFFT.Program.Main(String[] args) 位置 D:\☆FORESTHINK180925备份\001雷达仿真\编程学习\csharp\CsharpMatlabDemo\CsharpMatlabFFT\CsharpMatlabFFT\CsharpMatlabFFT\Program.cs:行号 27

 

相关文章:

  • 2021-11-26
  • 2021-05-14
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2021-10-22
  • 2021-09-19
猜你喜欢
  • 2021-08-17
  • 2021-05-16
  • 2022-02-20
  • 2022-01-14
  • 2022-12-23
  • 2021-09-29
  • 2021-12-28
相关资源
相似解决方案