Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling Matlab function from c# + OOP

trying to call a Matlab function from c# by using the instructions in the following Matlab help website.

https://se.mathworks.com/help/matlab/matlab_external/call-matlab-function-from-c-client.html

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);

>Solution :

You are assigning a result of Class1.FromMatLab(a, b, c); to a variable of type object, which doesn’t have X or Y property. If you change object data to Data data or var data, the compiler error should disappear.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading