MATLAB: How to create a UInt16 MWArray in .NET

mwarraynet

I am having problems using .NET Framework 4 with a compiled library using R2015b. I am trying to create an MWArray of UInt16 values (the library requires this and errors out on doubles and signed integers), but I am getting the error message "The data array type is invalid." when try either of these methods:
UInt16[] inputBytes;
/// ... Fill array...
MWArray mwInputData = (MWNumericArray)inputBytes;
MWArray mwInputData2 = new MWNumericArray(inputBytes);
We have used the same Matlab code in a C++ DLL library, and been able to create the MWArray like this:
unsigned short* raw_pixels;
/// ... Fill array...
mwInputData = mwArray(pixel_count, 1, mxUINT16_CLASS, mxREAL);
mwInputData.SetData(raw_pixels, pixel_count);
I am unable to find a way to create a typed MWArray in .NET and still be able to fill it, the only constructor that accepts a type (i.e., MWNumericType.UInt16) does not initialize the size (as far as I can tell), and when I try to set a value inside the array I get an index error.
Any ideas?
Thank you, -Andy

Best Answer

In case anybody else has this issue, I received the following work around from Mathworks support:
UInt16[] d = new UInt16[10];
d[0] = UInt16.MaxValue;
MWNumericArray ui = new MWNumericArray(MWArrayComplexity.Real, MWNumericType.UInt16, new int[] { 10, 1 });
for (int i=0;i<10;i++) {
ui[i+1, 1] = d[i];
}