-
Notifications
You must be signed in to change notification settings - Fork 4
/
MatlabFileWriter.cs
110 lines (98 loc) · 3.93 KB
/
MatlabFileWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ionic.Zlib;
namespace MatlabFileIO
{
public class MatlabFileWriter
{
BinaryWriter fileWriter;
MemoryStream uncompressedStream;
IMatlabFileWriterLocker locker;
public MatlabFileWriter(string fileName)
{
//create stream from filename
FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
this.fileWriter = new BinaryWriter(fileStream);
//write .mat file header (128 bytes)
fileWriter.WriteMatlabHeader();
}
public void Close()
{
Flush();
fileWriter.Close();
}
private void Flush()
{
if(uncompressedStream == null)
return;
fileWriter.Write(MatfileHelper.MatlabDataTypeNumber(typeof(ZlibStream)));
byte[] compressedBuffer = ZlibStream.CompressBuffer(uncompressedStream.ToArray());
fileWriter.Write((UInt32)compressedBuffer.Length);
fileWriter.Write(compressedBuffer);
uncompressedStream = null;
}
public MatLabFileArrayWriter OpenArray(Type t, string varName, bool compress)
{
//first check if there is no array operation ongoing
if (locker != null)
{
if(!locker.HasFinished())
throw new Exception("Previous array still open!");
Flush();
}
//check whether type is not a string, as this is not supported for now
if (t.Equals(typeof(String)))
throw new NotImplementedException("Writing arrays of strings is not supported (as strings are arrays already)");
MatLabFileArrayWriter arrayWriter;
if(compress) {
uncompressedStream = new MemoryStream();
arrayWriter = new MatLabFileArrayWriter(t, varName, new BinaryWriter(uncompressedStream));
} else {
arrayWriter = new MatLabFileArrayWriter(t, varName, fileWriter);
}
locker = (IMatlabFileWriterLocker)arrayWriter;
return arrayWriter;
}
public void Write(string name, object data, bool compress = true)
{
//let's write some data
if(data.GetType().Equals
(typeof(String))) {
//a string is considered an array of chars
string dataAsString = data as string;
MatLabFileArrayWriter charArrayWriter = OpenArray(typeof(char), name, compress);
charArrayWriter.AddRow(dataAsString.ToCharArray());
charArrayWriter.FinishArray(typeof(char));
}
else{
Type t;
MatLabFileArrayWriter arrayWriter;
if (data.GetType().IsArray && (data as Array).Rank > 1) //Handle multidimensional array
{
Array arr = data as Array;
if (arr.Rank > 2)
throw new Exception("Matlab write doesn't support multidimensional arrays with a rank > 2");
t = arr.GetValue(0, 0).GetType();
arrayWriter = OpenArray(t, name, compress);
for (int i = 0; i < arr.GetLength(0); i++)
{
arrayWriter.AddRow(arr.SliceRow(i));
}
}
else
{
if (data.GetType().IsArray)
t = (data as Array).GetValue(0).GetType();
else
t = data.GetType();
arrayWriter = OpenArray(t, name, compress);
arrayWriter.AddRow(data);
}
arrayWriter.FinishArray(t);
}
}
}
}