如何从c#调用matlab函数

问题描述 投票:1回答:1

我正在使用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.
c# matlab
1个回答
0
投票

通过https://github.com/jramshur/HRVAS/blob/master/freqDomainHRV.m判断第一个选项应该是2Darray(ibi = 2Dim时间序列和帧间间隔(s))。

但是你规定一维数组作为输入(变量res),因此我相信错误。

作为@KevinK。和@CrisLuengo建议尝试在不改变Matlab测试用例的任何参数的情况下测试您的函数。