3

I currently try to convert a .mat-file to XML. For this task, I have to use a library, which gives me a dynamic object back. I know the structure of the .mat-file, so I can get the data out of it. I store this data in an Object. One of the values of the .mat file is of the MATLAB-type <1701x256 double>. I thought this would be double[][]. But when I try to assign the value, I get:

Unbehandelte Ausnahme: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Der 
double[*,*]-Typ kann nicht in double[][] konvertiert werden.
   bei CallSite.Target(Closure , CallSite , Object )
   bei System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site,T0 arg0)
   bei CameraParser.Program.Main(String[] args) in c:\myProject\Program.cs:Zeile 44.

What is double[*,*] for a type? Of which type should the attribute of the object to which I assign the value to, be?

I tried

double[][] myAttribute;

and

double[] myAttribute;

and

double** myAttribute;

The last one gave

Error   6   Pointers and fixed size buffers may only be used in an unsafe context
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • Thanks cdhowie, `double[,]` seems to be correct. However, it can't be serialized :-( Do you know how to make it serializable? – Martin Thoma Jan 18 '13 at 18:56
  • And: Is the C-equivalent to `double[,]` this: `double**`? – Martin Thoma Jan 18 '13 at 18:57
  • No, `double**` is akin to `double[][]`. The closest C representation of `double[,]` in terms of marshaling is `double*`. – cdhowie Jan 18 '13 at 18:58
  • @moose - Multidimensional arrays should be serializable, as long as the array's element type is serializable. Are you using a custom serializer? – keyboardP Jan 18 '13 at 18:59
  • I use `System.Xml.Serialization.XmlSerializer` - but I'm new to C#, so if there is something more appropriate I could change this. – Martin Thoma Jan 18 '13 at 19:00
  • 1
    @moose - I believe `XmlSerializer` doesn't directly support multidimensional array serialization (happy to be corrected), but you can try using [BinaryFormatter](http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx) unless you need your serialization to be in XML format? – keyboardP Jan 18 '13 at 19:03
  • By the way, does this `double[,]` notation / data structure have a name? It's hard to google for something with special characters. – Martin Thoma Jan 18 '13 at 19:03
  • 1
    @moose - It's a 2D array, but can just as well be a multidimensional array (double[,,] would have 3 dimensions). Technically a 2D array is a multidimensional array since it has more than one dimensions, but I see it referred to a 2D array more often. – keyboardP Jan 18 '13 at 19:04
  • @keyboardP Thanks for mentioning `BinaryFormatter`! I have to think about it, but I'm quite sure this is a much better solution than using XML. – Martin Thoma Jan 18 '13 at 19:06
  • 1
    @moose - I just found [this example for the BF](http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/90c98754-2580-404a-81ae-aedba5f2604d/) but there's also [this post](http://stackoverflow.com/questions/553824/serializing-an-array-of-integers-using-xmlserializer) for XML. Might help :) – keyboardP Jan 18 '13 at 19:07

1 Answers1

3

The * in the type name indicates that you got a non-conforming array type back. Common with COM servers in particular, they tend to use 1 as the lower bound. You cannot cast such an array to a C# array type, it only supports arrays with a lower bound of 0. But you can cast it to Array.

Use the Array methods to access the array. Like Array.GetLowerBound() tells you where to start indexing, GetUpperBound() to find out where to stop. Read an array element with Array.GetValue(). You'll need the overload that takes int[] since this is a two-dimensional array and not a jagged array.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536