- Structure
- Sparse array
- Multidimensional SAFEARRAYs (greater than two dimensions)
- Write-only properties
Using Feval from C# - handling returned struct
8 views (last 30 days)
Show older comments
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();
}
0 Comments
Answers (1)
Preethi Ayyamperumal
on 6 Jun 2018
MATLAB does not support the following COM interface types:
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 Comment
javid akhavan
on 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
See Also
Categories
Find more on .NET Client Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!