如何将数组从c#发送到matlab脚本?

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

我想传递多个数组作为 matlab 函数的输入并在 .net6 Web 应用程序中接收结果?如何做到这一点?

这是一个matlab代码示例

function  [a, b, c] = myfunc(x)    % where x is an array
    a = x(1)+x(2);        % access array elements
    b = a;
    c = prod(x);          % x1*x2*x3...xn;
end 

c# 代码中的这个:

MLApp.MLApp matlab = new MLApp.MLApp();

matlab.Execute(@"cd C:\Users\user\Desktop\myfunc.m");

object result = null;

int[] input = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 500, 800, 14, 78, 99, 45, 201, 336, 5578 };
matlab.Feval("myfunc", 2, out result, input);

// Display result 
object[] res = result as object[];

Console.WriteLine(res[0]);
Console.WriteLine(res[1]);
Console.WriteLine(res[3]);
Console.ReadLine();

这给了我一个例外:

matlab.Feval("myfunc", 2, out result, input);

例外是:类型为“int32”的输入参数未定义函数“myfunc”

c# matlab matlab-deployment
1个回答
0
投票

当我改变这条线时它起作用了

matlab.Execute(@"cd C:\Users\user\Desktop\myfunc.m");

matlab.Execute(@"cd C:\Users\user\Desktop");

并更改此行中从 matlab 函数返回的输出数量

matlab.Feval("myfunc", 3, 输出结果, 输入);

© www.soinside.com 2019 - 2024. All rights reserved.