C# Matlab 相互調用

來源:互聯網
上載者:User

標籤:調用   demo   info   cal   ==   compiler   ret   lcm   sel   

 

C# Matlab 相互調用

發表於2016/7/28 17:21:42  3551人閱讀

分類: 混合編程

C# Matlab 相互調用

測試環境

VisualStudio2013 / .net4.0 

Matlab2015b

高版本的matlab對外接其它語言做得很方便了,並不需要一堆的配置。 

其它語言與matlab的互動操作也類似。

C#調用Matlab

基本思路:將matlab函數打包成DLL檔案,聯合matlab資料支援DLL(MWArray.dll),交付給其它語言程式使用。

1、Matlab端的操作

編寫matlab函數:

function [result,m,n] = GetSelfMultiplyResult(list)
% 計算 矩陣與其轉置矩陣的乘積
% 測試返回多個結果
result = list*list‘;
[m,n] = size(result);
end

·        function result = GetSelfSquareResult(list)
% 計算 矩陣各元素平方後的結果
result = list.^2;
end

·        打包函數:

·        找到庫編譯器(LibraryCompiler) 

 

·        打包函數 
如所示, 
1)選擇目標類型(TYPE); 
2)添加需要打包的函數檔案; 
3)重新命名庫名稱。 

 

3.    重新命名類名稱,或者添加類,分配函數所屬類。完成打包操作。 

 

匯出DLL

在產生的檔案中,找到“for_redistribution_files_only”檔案夾,裡面有 

CalcMatResult.dll CalcMatResultNative.dll 兩個dll檔案,均可使用。 

另外,MWArray.dll 在matlab安裝目錄中,參考路徑:

X:\Program Files\MATLAB\R2015b\toolbox\dotnetbuilder\bin\win64\v2.0\

·        1

也可以直接使用Everything等軟體直接搜尋。

2、C#端的操作

C#端用到的就是 MWArray.dll 和 CalcMatResultNative.dll 這兩個DLL檔案。

·        添加DLL引用 

 

·        示範代碼

using System;
using CalcMatResultNative; //添加引用
using MathWorks.MATLAB.NET.Arrays; //添加引用

namespaceCsharpMatlabDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] list ={{1},{2},{3},{4}}; //列向量

MWArray array = newMWNumericArray(list);

CalcMatResultNative.Multiply multi= new Multiply();
            object resultObj =multi.GetSelfMultiplyResult(3, array);// 3 表示返回的結果數量,要小於等於matlab對應函數實際的傳回值數量
            object[] resultObjs =(object[]) resultObj;

double[,] calcResult= (double[,])resultObjs[0];
            double[,] sizem = (double[,])resultObjs[1];
            double[,] sizen = (double[,])resultObjs[2];

Console.ReadKey();
        }
    }
}

Matlab調用C#

matlab調用C#更加簡單,先將C#代碼編譯成dll庫,matlab中直接引用即可調用。

如果失敗,注意檢查使用的.net版本是否過高,平台(x64/86)是否匹配等問題。 

注意選擇Release版本的DLL(C#的Bebug版本也可以引用,但C++的不行)。

1、C#端操作

代碼

namespace MatlabInterface
{
    public class Dialog
    {
        public static bool ShowSelectMsg(string msg, string title)
        {
            DialogResult r =MessageBox.Show(msg, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            return r == DialogResult.Yes;
        }

public string Msg { get; set; }

public void ShowMsg()
        {
            MessageBox.Show(Msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

}
}

 

2、Matlab操作

% 調用C# dll

% 引用絕對路徑
NET.addAssembly(‘R:\Users\GrassPhy\Desktop\MatlabCsharpDemo\MatlabInterface.dll‘);

% 靜態方法調用
select = MatlabInterface.Dialog.ShowSelectMsg(‘請選擇...‘,‘提示‘);
if select
    disp(‘yes‘);
else
    disp(‘no‘);
end

% 成員方法調用
dialog = MatlabInterface.Dialog();
dialog.Msg = ‘提示資訊‘;
dialog.ShowMsg();

參考: 

C#中使用MATLAB

 

C# Matlab 相互調用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.