Effacer les filtres
Effacer les filtres

Using Feval from C# - handling returned struct

20 vues (au cours des 30 derniers jours)
Jeremy Howard-Baynham
Jeremy Howard-Baynham le 29 Mai 2018
Commenté : javid akhavan le 17 Jan 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();
}

Réponses (1)

Preethi Ayyamperumal
Preethi Ayyamperumal le 6 Juin 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 commentaire
javid akhavan
javid akhavan le 17 Jan 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Package MATLAB Functions dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by