Borrar filtros
Borrar filtros

Using Feval from C# - handling returned struct

16 visualizaciones (últimos 30 días)
Jeremy Howard-Baynham
Jeremy Howard-Baynham el 29 de Mayo de 2018
Comentada: javid akhavan el 17 de En. de 2020
Using the sample code on the website I can call a simple function successfully, but where the returned type is a struct, all I get back is a null, but with no error.
Sample function:
function out = myfunc(y)
out.mean = mean(y); % mean
out.median = median(y); % median
out.std = std(y); % standard deviation
end
My C# code:
static void Main(string[] args)
{
MLApp.MLApp matlab = new MLApp.MLApp();
matlab.Execute(@"cd c:\BF_hctsa\Operations");
object result = null;
System.Array input = new double[10];
for (int i = 0; i < 10; i++)
{
input.SetValue(1.0, i);
}
matlab.Feval("myfunc", 1, out result, input);
object[] res = result as object[];
Console.WriteLine(res[0]);
Console.ReadLine();
}

Respuestas (1)

Preethi Ayyamperumal
Preethi Ayyamperumal el 6 de Jun. de 2018
MATLAB does not support the following COM interface types:
  • Structure
  • Sparse array
  • Multidimensional SAFEARRAYs (greater than two dimensions)
  • Write-only properties
The workaround is to return fields of the desired struct from the MATLAB function when using Feval. Please refer to the updated code below:
MATLAB Code:
function [out.mean,out.median,out.std] = myfunc(y)
out.mean = mean(y); % mean
out.median = median(y); % median
out.std = std(y); % standard deviation
end
C# code
static void Main(string[] args)
{
MLApp.MLApp matlab = new MLApp.MLApp();
matlab.Execute(@"cd c:\BF_hctsa\Operations");
object result = null;
System.Array input = new double[10];
for (int i = 0; i < 10; i++)
input.SetValue(1.0, i);
matlab.Feval("myfunc", 3, out result, input);
object[] res = result as object[];
Console.WriteLine(res[0]);
Console.WriteLine(res[1]);
Console.WriteLine(res[2]);
Console.ReadLine();
}
  1 comentario
javid akhavan
javid akhavan el 17 de En. de 2020
How do you read the output? I mean I nead to work with the data in res[0] or others, but since they are "objects" I can't use them.
Consider res[0] is soppused to be an Integer
and i want to compare it to 1,
I wrote
if (res[0] == 1)
....
but it wouldn't work

Iniciar sesión para comentar.

Categorías

Más información sobre Package MATLAB Functions en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by