如何从C#调用Matlab函数

时间:2018-09-10 14:38:19

标签: c# matlab

我正在用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.

1 个答案:

答案 0 :(得分:0)

https://github.com/jramshur/HRVAS/blob/master/freqDomainHRV.m判断 第一个选项应该是2Darray(ibi = 2Dim时间(s)和心拍间隔(s)的数组)。

但是,您将一维数组指定为Input(变量res),因此是我认为的错误。

就像@KevinK。和@CrisLuengo建议尝试在不更改Matlab测试用例的任何参数的情况下测试您的功能。