簡體   English   中英

如何從C#調用Matlab函數

[英]How to call matlab function from c#

我正在用Matlab構建示例函數。 函數是這樣的:

function output = freqDomainHRV(ibi,VLF,LF,HF,AR_order,window, ...
    noverlap,nfft,fs,methods,flagPlot)
   //code

end

現在,要調用此函數,我可以使用以下代碼:

output = freqDomainHRV(sampledata,[0 .16],[.16 .6],[.6 3], ...
%                       16,256,128,512,10,{'welch','ar','lomb'},1);

現在,我想從C#程序調用此函數,因此我要從matlab構建dll,並將其導入到我的visual studio項目中,然后編寫以下代碼:

private void calculateFreqDomainHRV()
        {
            //costruisco l'array contenente i valori dell ECG
            MWNumericArray res = new MWNumericArray(new double[] { 0.789, 0.828, 0.844, 0.820 });
            MWNumericArray vlf = new MWNumericArray(new double[] { 0, 0.04 });
            MWNumericArray lf = new MWNumericArray(new double[] { 0.04, 0.15 });
            MWNumericArray hf = new MWNumericArray(new double[] { 0.15, 0.40 });
            MWCellArray method = new MWCellArray(3);
            method[1] = new MWCharArray("welch");
            method[2] = new MWCharArray("ar");
            method[3] = new MWCharArray("lomb");
            var arOptionOrder = 16;
            var winWith = 128;
            var winOverlap = 64;
            var pointPSD = 1024;
            var interpolationRate = 2;
            /*freqDomainHRV(sampledata,[0 .16],[.16 .6],[.6 3], ...
% 16, 256, 128, 512, 10);*/
            var output = matlab.freqDomainHRV(res, vlf, lf,hf, arOptionOrder, winWith, winOverlap, pointPSD, interpolationRate, method,1);
}

但是,如果我嘗試調用此方法,則會收到以下錯誤消息:

. MWMCR::EvaluateFunction error ... 
Interpolation requires at least two sample points in each dimension.
Error in => freqDomainHRV.m at line 97.

... Matlab M-code Stack Trace ...
    at
file D:\MATLAB\R2018a\mcr\toolbox\matlab\polyfun\interp1.p, name interp1, line 0.
    at
file C:\Users\michele.castriotta\AppData\Local\Temp\mik\mcrCache9.4\Matlab2\MatlabECG\freqDomainHRV.m, name calcWelch, line 145.
    at
file C:\Users\michele.castriotta\AppData\Local\Temp\mik\mcrCache9.4\Matlab2\MatlabECG\freqDomainHRV.m, name freqDomainHRV, line 97.

通過https://github.com/jramshur/HRVAS/blob/master/freqDomainHRV.m判斷,第一個選項應該是2Darray(ibi = 2Dim時間(s)和心跳間隔(s))。

但是,您將一維數組指定為Input(變量res ),因此我相信該錯誤。

作為@KevinK。 和@CrisLuengo建議嘗試在不更改Matlab測試用例的任何參數的情況下測試您的功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM