0

I am currently using this preliminary code:

static void Main(string[] args)
{
    try
    {
    Type matlabtype;
    matlabtype = Type.GetTypeFromProgID("matlab.application");

    object matlab;
    matlab = Activator.CreateInstance(matlabtype);

    Execute(matlabtype, matlab, "clear;");
    Execute(matlabtype, matlab, "path(path,'H:/bla/bla');");
    Execute(matlabtype, matlab, "Object = ClassName();");
    Execute(matlabtype, matlab, "Object.parameter1 = 100;");
    Execute(matlabtype, matlab, "Object.parameter2 = 300;");
    object o = Execute(matlabtype, matlab, "Object.ComputeSomething()");
    }
    catch (Exception e)
    {

    }
}

to create an object of a particular class, set some properties and compute something. Here:

ComputeSomething();

returns a scalar.

I am just wondering whether this is the best way to program this and what’s the cleanest way to obtain the actual scalar value without using string operations (e.g. remove ans =)?

Thanks.

Christian

cs0815
  • 16,751
  • 45
  • 136
  • 299
  • http://stackoverflow.com/questions/434331/interoperating-between-matlab-and-c-sharp helpful? – Christoph Jul 23 '12 at 15:56
  • From [here](http://gilbertmultiplier.blogspot.com/2009/03/how-to-call-matlab-in-c.html): have you tried the `GetFullMatrix` command? – tmpearce Jul 23 '12 at 16:31

1 Answers1

0

You can retrieve data from matlab using a few commands. To get a scalar you can call GetVariable.

Execute(matlabtype, matlab, "result = Object.ComputeSomething()");
GetVariable(matlabtype, matlab, "result", "base")

see Call MATLAB COM Automation Server for available calls.