C#中部署的Matlab DLL出错(位置2的索引超出了数组范围(必须不超过1))

时间:2019-10-01 08:25:04

标签: c# .net matlab matlab-deployment

我将Matlab函数部署到了.NET程序集,现在我在实现上遇到了一些问题。

我的GUI告诉我,第12行的.m文件中存在错误,但我没有真正看到此问题。 希望你能帮助我!

这是我的C#代码,它使用已部署的.NET程序集(渐变)和Mathworks API的一部分:

using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using Gradient;

namespace matlabv3
{
    public class Gradientenberechnung
    {
        public static void addNum()
        {
            int pdata = 250;

            CSharpGradient gradient = new CSharpGradient();
            MWArray output = gradient.gradientVarianteA((MWArray)pdata); //<-- Error happens here

            Console.WriteLine("Output: ", output);
            Console.ReadKey();

        }
    }
}

这是我的Matlab函数,它返回数学梯度:

function [outputArg] = gradientVarianteA(pdata)

%% Variante A - Only the maximum difference is calculated without division by the distance
    d = nan(10,9);
    for n = 1:numel(pdata)
        [j,i] = ind2sub(size(pdata),n);
        switch true
            case j==1
                if i == 1
                    d(j,i) = max(abs([...
                        NaN                         NaN                     NaN...
                        NaN                         NaN                     pdata(j,i)-pdata(j,i+1)...
                        NaN                         pdata(j,i)-pdata(j+1,i) pdata(j,i)-pdata(j+1,i+1)]));
                elseif i == 9
                    d(j,i) = max(abs([...
                        NaN                         NaN                     NaN...
                        pdata(j,i)-pdata(j,i-1)     NaN                     NaN...
                        pdata(j,i)-pdata(j+1,i-1)   pdata(j,i)-pdata(j+1,i) NaN]));
                else
                    d(j,i) = max(abs([...
                        NaN                         NaN                     NaN...
                        pdata(j,i)-pdata(j,i-1)     NaN                     pdata(j,i)-pdata(j,i+1)...
                        pdata(j,i)-pdata(j+1,i-1)   pdata(j,i)-pdata(j+1,i) pdata(j,i)-pdata(j+1,i+1)]));
                end
            case j==10
                if i == 1
                    d(j,i) = max(abs([...
                        NaN                         pdata(j,i)-pdata(j-1,i) pdata(j,i)-pdata(j-1,i+1)...
                        NaN                         NaN                     pdata(j,i)-pdata(j,i+1)...
                        NaN                         NaN                     NaN]));
                elseif i == 9
                    d(j,i) = max(abs([...
                        pdata(j,i)-pdata(j-1,i-1)   pdata(j,i)-pdata(j-1,i) NaN...
                        pdata(j,i)-pdata(j,i-1)     NaN                     NaN...
                        NaN                         NaN                     NaN]));
                else
                    d(j,i) = max(abs([...
                        pdata(j,i)-pdata(j-1,i-1)   pdata(j,i)-pdata(j-1,i) pdata(j,i)-pdata(j-1,i+1)...
                        pdata(j,i)-pdata(j,i-1)     NaN                     pdata(j,i)-pdata(j,i+1)...
                        NaN                         NaN                     NaN]));
                end
            otherwise
                if i == 1
                    d(j,i) = max(abs([...
                        NaN                         pdata(j,i)-pdata(j-1,i) pdata(j,i)-pdata(j-1,i+1)...
                        NaN                         NaN                     pdata(j,i)-pdata(j,i+1)...
                        NaN                         pdata(j,i)-pdata(j+1,i) pdata(j,i)-pdata(j+1,i+1)]));
                elseif i == 9
                    d(j,i) = max(abs([...
                        pdata(j,i)-pdata(j-1,i-1)   pdata(j,i)-pdata(j-1,i) NaN...
                        pdata(j,i)-pdata(j,i-1)     NaN                     NaN...
                        pdata(j,i)-pdata(j+1,i-1)   pdata(j,i)-pdata(j+1,i) NaN]));
                else
                    d(j,i) = max(abs([...
                        pdata(j,i)-pdata(j-1,i-1)   pdata(j,i)-pdata(j-1,i) pdata(j,i)-pdata(j-1,i+1)...
                        pdata(j,i)-pdata(j,i-1)     NaN                     pdata(j,i)-pdata(j,i+1)...
                        pdata(j,i)-pdata(j+1,i-1)   pdata(j,i)-pdata(j+1,i) pdata(j,i)-pdata(j+1,i+1)]));
                end
        end
    end

outputArg = d;
end

0 个答案:

没有答案