MATLAB: Is there an example on how to use FEVAL from C# code

activatorccallingfevalMATLAB

I would like to call MATLAB from my C# code and to use the FEVAL method. Do you have an example on how I can perform that?

Best Answer

Below is an example showing the use of FEVAL from C# code:
add these two lines at the beginning
using System;
using System.Reflection;
// Create or get an instance of MATLAB
Type matlabType = Type.GetTypeFromProgID("Matlab.Application");
object matlab = Activator.CreateInstance(matlabType);
// Object[] parameters = new Object[] { functionName, nargout, Result, arg1, arg2, ... argN };
Object[] parameters = new Object[] { "strcat", 1, null, "Hello", " World!" };
// Make 3rd parameter (the return value) be passed by reference
ParameterModifier mods = new ParameterModifier(parameters.Length);
mods[2] = true;
try
{
// Call Feval to evaluate the result of "strcat" function
matlabType.InvokeMember("Feval",
BindingFlags.InvokeMethod,
null,
matlab,
parameters,
new ParameterModifier[] { mods },
null,
null);
// Get the return value
object Result = ((object[])parameters[2])[0];
// Print the return value
Console.WriteLine(Result);
}
catch (TargetInvocationException e)
{
Console.WriteLine(e.Message);
}