Using Feval from C# - handling returned struct

7 次查看(过去 30 天)
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();
}

回答(1 个)

Preethi Ayyamperumal
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 个评论
javid akhavan
javid akhavan 2020-1-17
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Package MATLAB Functions 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by