Matlab Coder input arguments? (variable array size)

5 views (last 30 days)
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 Comment
Ryan Livingston
Ryan Livingston on 13 May 2014
Edited: Ryan Livingston on 14 May 2014
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.

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB Code Analysis in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!