Matlab Coder input arguments? (variable array size)

7 次查看(过去 30 天)
Hello! I am trying to export some code done in Matlab to a C# project via the matlab Coder and compiling it into a .dll file. It works nicely for handling arrays of a set size but when I choose the input argument in my Entry-Point Files to be an array of variable size "Up to 4 (:4)" something goes wrong.
I have previously tested returning a set sized array "Exactly 4 (4)" which works fine.
Here is the Matlab code, quite simple:
function [ out ] = divideArray( in )
out = in / 2;
end
Here is the extern function that is generated in the divideArray.h file:
extern void divideArray(const real_T in_data[4], const int32_T in_size[2], real_T out_data[4], int32_T out_size[2]);
Here is the C# code, first the DLL import declaration:
private static extern void divideArray( [In] double[] arrIn,
[In] int[] inSize,
[Out] double[] arrOut,
[Out] int[] outSize);
And then the C# main funtion:
static void Main(string[] args)
{
double[] foo = new double[4] { 1, 4, 9, 43 };
double[] bar = new double[4];
int[] inSize = new int[2] { 1, 4 };
int[] outSize = new int[2];
divideArray(foo, inSize, bar, outSize);
Console.WriteLine("\ninSize:");
Console.WriteLine("Length: " + inSize.Length);
Console.WriteLine("[0]: " + inSize.GetValue(0));
Console.WriteLine("[1]: " + inSize.GetValue(1));
Console.WriteLine("\noutSize:");
Console.WriteLine("Length: " + outSize.Length);
Console.WriteLine("\nbar values:");
for (int i = 0; i < 4; i++)
{
Console.Write(foo.GetValue(i) + " ");
}
Console.WriteLine("\n\nDone.");
Console.ReadKey();
}
Here is the console output I get:
inSize:
Length: 2
[0]: 0
[1]: 1071644672
outSize:
Length: 0
bar values:
0 0 0 0
Done.
So not quite sure what happens, it works perfectly fine when the input array size is set to a constant but when it is variable the input size is changed and no output is made. I have tested a lot of different inSize values in case the array indexing was Matlab or C but no difference. And again, it works with no problems when the array size is set to a constant :)
Thanks in advance, Viktor
  1 个评论
Ryan Livingston
Ryan Livingston 2014-5-13
编辑:Ryan Livingston 2014-5-14
My knowledge of C# is limited. Do you know how it is that inSize is being modified from {1,4} to the values shown? Does inSize have those values before the call to divideArray?
Also, the values printed seem to be from foo rather than bar:
Console.Write(foo.GetValue(i) + " ");
Do you know how the value of foo would have been modified? My suggestion would be to see if the proper values are even making it to divideArray.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Code Analysis 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by