I am trying to call a Matlab function from c# by using the instructions in this Matlab help website.
Using the method and the example in the Matlab website went well but now trying to redo everything with writing a separated class for calling the Matlab function, and call the method in the main class and give the parameters values to get results.
When doing so, an error in reusing the variables happend.
The following is the class that is calling the Matlab function.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fromMatlab
{
public class Class1
{
public static Data FromMatLab(double a, double b, string c)
{
MLApp.MLApp matlab = new MLApp.MLApp();
matlab.Execute(@"cd c:\Users\abdhab\Documents\MATLAB");
object result = null;
matlab.Feval("myfunc", 2, out result, 3.14, 42.0, "world");
object[] res = result as object[];
object x = res[0];
object y = res[1];
return new Data { X = x, Y = y };
}
}
}
The class that deals with the variables and the constructers.
namespace fromMatlab
{
public class Data
{
public object X { get; set; }
public object Y { get; set; }
}
public struct DataStruct
{
object X;
object Y;
}
}
The Main class.
using System;
using System.Collections.Generic;
using System.Text;
namespace fromMatlab
{
class Program
{
static void Main(string[] args)
{
double a = 1;
double b = 2;
string c = "world";
object data =Class1.FromMatLab(a, b, c);
object X = data.X;
object Y = data.Y;
Console.WriteLine(X);
Console.WriteLine(Y);
Console.ReadLine();
}
}
}
The error is the compiler error CS1061 and it occur in the following lines.
object X = data.X;
object Y = data.Y;
The Matlab function is the following:
function [x,y] = myfunc(a,b,c)
x= a+b;
y = sprintf('Hello %s',c);