필터 지우기
필터 지우기

Using Feval from C# - handling returned struct

조회 수: 16 (최근 30일)
Jeremy Howard-Baynham
Jeremy Howard-Baynham 2018년 5월 29일
댓글: javid akhavan 2020년 1월 17일
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
Preethi Ayyamperumal 2018년 6월 6일
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