/// <summary> /// Convert MatFileType enumeration member to string representation /// </summary> /// <param name="type">MatFileType enumeration member</param> /// <returns>String representing the Matlab's inner element type</returns> /// <remarks>This function is obsolete. You may directly use the enumeration's functionality instead.</remarks> public static String typeToString(MatFileType type) { String s; switch (type) { case MatFileType.miUNKNOWN: s = "unknown"; break; case MatFileType.miINT8: s = "int8"; break; case MatFileType.miUINT8: s = "uint8"; break; case MatFileType.miINT16: s = "int16"; break; case MatFileType.miUINT16: s = "uint16"; break; case MatFileType.miINT32: s = "int32"; break; case MatFileType.miUINT32: s = "uint32"; break; case MatFileType.miSINGLE: s = "single"; break; case MatFileType.miDOUBLE: s = "double"; break; case MatFileType.miINT64: s = "int64"; break; case MatFileType.miUINT64: s = "uint64"; break; case MatFileType.miMATRIX: s = "matrix"; break; case MatFileType.miCOMPRESSED: s = "compressed"; break; case MatFileType.miUTF8: s = "uft8"; break; case MatFileType.miUTF16: s = "utf16"; break; case MatFileType.miUTF32: s = "utf32"; break; default: s = "unknown"; break; } return s; }
/// <summary> /// read array of supported matlab data types /// </summary> /// <param name="br">binary reader, opened and correctly positioned</param> /// <param name="storageType">actual storage type</param> /// <param name="realData">output: on return, the array read</param> /// <param name="len">input: number of bytes to read, on return: number of elements in array</param> /// <param name="paddBytes">padding border, the stream will be read to the next border of length 'paddBytes'.</param> private static void readElementGeneric(BinaryReader br, MatFileType storageType, out System.Array realData, ref Int32 len, int paddBytes) { Int32 readInt = len; switch (storageType) { case MatFileType.miINT8: realData = System.Array.CreateInstance(typeof(byte), readInt / sizeOf(storageType)); len = realData.Length; realData = br.ReadBytes(len); while (len % paddBytes != 0) { br.ReadByte(); len++; } break; case MatFileType.miUINT8: realData = System.Array.CreateInstance(typeof(char), readInt / sizeOf(storageType)); len = realData.Length; realData = br.ReadChars(len); while (len % paddBytes != 0) { br.ReadByte(); len++; } break; case MatFileType.miINT16: realData = System.Array.CreateInstance(typeof(short), readInt / sizeOf(storageType)); len = realData.Length; for (int i = 0; i < len; i++) { realData.SetValue(br.ReadInt16(), i); } while ((len * sizeOf(storageType) % paddBytes) != 0) { len++; br.ReadInt16(); } break; case MatFileType.miUINT16: realData = System.Array.CreateInstance(typeof(UInt16), readInt / sizeOf(storageType)); len = realData.Length; for (int i = 0; i < len; i++) { realData.SetValue(br.ReadUInt16(), i); } while ((len * sizeOf(storageType) % paddBytes) != 0) { len++; br.ReadUInt16(); } break; case MatFileType.miINT32: realData = System.Array.CreateInstance(typeof(Int32), readInt / sizeOf(storageType)); len = realData.Length; for (int i = 0; i < len; i++) { realData.SetValue(br.ReadInt32(), i); } while ((len * sizeOf(storageType) % paddBytes) != 0) { len++; br.ReadInt32(); } break; case MatFileType.miUINT32: realData = System.Array.CreateInstance(typeof(UInt32), readInt / sizeOf(storageType)); len = realData.Length; for (int i = 0; i < len; i++) { realData.SetValue(br.ReadUInt32(), i); } while ((len * sizeOf(storageType) % paddBytes) != 0) { len++; br.ReadUInt32(); } break; case MatFileType.miSINGLE: realData = System.Array.CreateInstance(typeof(float), readInt / sizeOf(storageType)); len = realData.Length; for (int i = 0; i < len; i++) { realData.SetValue(br.ReadSingle(), i); } while ((len * sizeOf(storageType) % paddBytes) != 0) { len++; br.ReadSingle(); } break; case MatFileType.miDOUBLE: realData = System.Array.CreateInstance(typeof(double), readInt / sizeOf(storageType)); len = realData.Length; for (int i = 0; i < len; i++) { realData.SetValue(br.ReadDouble(), i); } while ((len * sizeOf(storageType) % paddBytes) != 0) { len++; br.ReadDouble(); } break; case MatFileType.miINT64: realData = System.Array.CreateInstance(typeof(Int64), readInt / sizeOf(storageType)); len = realData.Length; for (int i = 0; i < len; i++) { realData.SetValue(br.ReadInt64(), i); } while ((len * sizeOf(storageType) % paddBytes) != 0) { len++; br.ReadInt64(); } break; case MatFileType.miUINT64: realData = System.Array.CreateInstance(typeof(UInt64), readInt / sizeOf(storageType)); len = realData.Length; for (int i = 0; i < len; i++) { realData.SetValue(br.ReadUInt64(), i); } while ((len * sizeOf(storageType) % paddBytes) != 0) { len++; br.ReadInt64(); } break; case MatFileType.miMATRIX: throw new NotSupportedException("matrix data type not expected as inner datatype!"); case MatFileType.miCOMPRESSED: throw new NotSupportedException("Compressed matrix are not supported (yet)! "); case MatFileType.miUTF8: realData = System.Array.CreateInstance(typeof(UTF8Encoding), readInt / sizeOf(storageType)); len = realData.Length; for (int i = 0; i < len; i++) { realData.SetValue(br.ReadUInt16(), i); } while ((len * sizeOf(storageType) % paddBytes) != 0) { len++; br.ReadInt16(); } break; case MatFileType.miUTF16: throw new NotSupportedException("UTF16 data type not supported (yet)!"); case MatFileType.miUTF32: realData = System.Array.CreateInstance(typeof(UTF32Encoding), readInt / sizeOf(storageType)); len = realData.Length; for (int i = 0; i < len; i++) { realData.SetValue(br.ReadChars(2), i); } while ((len * sizeOf(storageType) % paddBytes) != 0) { len++; br.ReadChars(2); } break; default: throw new Exception("Unknown element data type found! Cancelling..."); } }
/// <summary> /// size of single elements stored in Matlab's *.mat files /// </summary> /// <param name="type">one of Matlab's inner element types</param> /// <returns>size in bytes </returns> private static int sizeOf(MatFileType type) { switch ( type ) { case MatFileType.miINT8: return miSIZE_INT8; case MatFileType.miUINT8: return miSIZE_UINT8; case MatFileType.miINT16: return miSIZE_INT16; case MatFileType.miUINT16: return miSIZE_UINT16; case MatFileType.miINT32: return miSIZE_INT32; case MatFileType.miUINT32: return miSIZE_UINT32; case MatFileType.miDOUBLE: return miSIZE_DOUBLE; case MatFileType.miSINGLE: return miSIZE_SINGLE; case MatFileType.miUTF32: return miSIZE_UTF32; default: return 1; } }
private static void readElementGeneric(BinaryReader br, MatFileType storageType, out System.Array realData, ref Int32 len) { readElementGeneric (br,storageType,out realData, ref len, 8); }