Using MatLab function in C# and passing and receiving an array

4 次查看(过去 30 天)
I tried a lot to find a clear description about this issue but most of them do not cover my question.
How can I pass 2D array to function and receive it?
The following code is my MatLab function:
function V = Mfunction(f,U,A)
% f is a vector
% A is a double number
% u is a n x n matrix
% output V is a n x n matrix
n = length(U);
for i=2:n+1
for j=2:n+1
U2(i,j) = U(i+1,j)/A;
end
end
e = Mfunction(f,U2);
V = U + e;
This is my C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using Mfunction;
namespace matlabApplication
{
class Program
{
static void Main(string[] args)
{
int n = 100;
double[,] U = new double[n, n];
double[] f = new double[n];
// some calculations such as follow
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
U[i,j] = U[i,j] + 10;
f[i,j] = U[i,j] * 10;
}
}
*// Now I need to pass f & U into the Matalb function
// How?*
MLApp.MLApp matlab = new MLApp.MLApp();
matlab.Execute(@" cd C:\Users\jc262819\Documents\MATLAB");
*// After calculation I need to save the output of Matlab function (V) in an array in c#
// How?*
}
}
}

回答(1 个)

Varun Bhaskar
Varun Bhaskar 2015-8-10
Hello,
Refer to the following documentation links in order to learn how to retrieve data from MATLAB into your C# application:

类别

Help CenterFile Exchange 中查找有关 .NET Client Programming 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by