2015-10-16 56 views
1

我正在使用C#編寫的Matlab程序集。我得到了很多東西,但我遇到了一個問題。我有這樣一個類:如何在Matlab中處理非標準代表

classdef Mt4Class 
% stuff left out 
    methods  
    function self = Mt4Class(theIp, thePort) 
     self.IP = theIp; 
     self.Port = thePort; 
     NET.addAssembly('C:\Program Files (x86)\MtApi\MtApi.dll'); 
     self.apiClient = MtApi.MtApiClient(); 
    end 

    function AddListenerQuoteUpdated(self, callback) 
     addlistener(self.apiClient, 'QuoteUpdated', callback); 
    end 

    function MyQuoteUpdate(~, ~, symbol, bid, ask) 
     disp(symbol, bid, ask); 
    end 
end 
end 

然後我嘗試以下方法:

mtapi = Mt4Class('', 8222); 
mtapi.AddListenerQuoteUpdated(@(~,~, symbol, bid, ask)mtapi.MyQuoteUpdate(0, 0, symbol, bid, ask)); 

然而,MATLAB回來了一個錯誤:

.NET events with nonstandard delegate definition are not supported in MATLAB. 

我沒有源到Api。如果我這樣做了,將參數打包成EventArg會很簡單。我也可以在我自己的程序集中封裝API庫,以便在sender, eventargs表單中反映事件以符合Matlab,但這看起來很笨拙。有沒有解決的辦法?

+0

我在下面的解決方案中提供了一些更多的細節 –

回答

1

它似乎不是,我只是遇到了同樣的問題,但在我的情況下,我確實擁有發射事件的DLL。

看到

https://msdn.microsoft.com/en-us/library/w369ty8x.aspx

你可能想要做的是做這樣的事情我在做什麼。

基本上,您希望製作一個DLL包裝器,它將採用原始DLL中的Bid和Ask值,然後將它們放入一個返回該值的單個對象中。 ExpandoObjects聽起來很棒,但是我懷疑它們很容易在Matlab中用來獲取值。我使用一個字典來獲得對象。

編寫一個調用密封DLL的簡單DLL包裝器(您可以從本機DLL偵聽複雜事件)。然後在C#中創建一個Dictionary並添加原始的dict.Add(「core」,ddlldata),然後dict.Add(「additional」,more data)。

然後創建一個事件併發送這個字典到Matlab。你可以在Matlab中使用.ITEM(「key」).net對象的值操作並獲得兩個值。

如果您想查看示例,我可以提供代碼。