1 | function WriteData(fid,prefix,varargin)
|
---|
2 | %WRITEDATA - write model field in binary file
|
---|
3 | %
|
---|
4 | % Usage:
|
---|
5 | % WriteData(fid,varargin);
|
---|
6 |
|
---|
7 | %process options
|
---|
8 | options=pairoptions(varargin{:});
|
---|
9 |
|
---|
10 | %Get data properties
|
---|
11 | if exist(options,'object');
|
---|
12 | obj = getfieldvalue(options,'object');
|
---|
13 | fieldname = getfieldvalue(options,'fieldname');
|
---|
14 | classname = getfieldvalue(options,'class',class(obj));
|
---|
15 | name = getfieldvalue(options,'name',[prefix '.' fieldname ]);
|
---|
16 | if exist(options,'data'),
|
---|
17 | data = getfieldvalue(options,'data');
|
---|
18 | else
|
---|
19 | data = obj.(fieldname);
|
---|
20 | end
|
---|
21 | else
|
---|
22 | data = getfieldvalue(options,'data');
|
---|
23 | name = getfieldvalue(options,'name');
|
---|
24 | end
|
---|
25 | format = getfieldvalue(options,'format');
|
---|
26 | mattype = getfieldvalue(options,'mattype',0); %only required for matrices
|
---|
27 | timeserieslength = getfieldvalue(options,'timeserieslength',-1);
|
---|
28 |
|
---|
29 | %Process sparse matrices
|
---|
30 | if issparse(data),
|
---|
31 | data=full(data);
|
---|
32 | end
|
---|
33 |
|
---|
34 | %Scale data if necesarry
|
---|
35 | if exist(options,'scale'),
|
---|
36 | scale = getfieldvalue(options,'scale');
|
---|
37 | if size(data,1)==timeserieslength,
|
---|
38 | data(1:end-1,:) = scale.*data(1:end-1,:);
|
---|
39 | else
|
---|
40 | data = scale.*data;
|
---|
41 | end
|
---|
42 | end
|
---|
43 | if(size(data,1)==timeserieslength),
|
---|
44 | yts = getfieldvalue(options,'yts');
|
---|
45 | data(end,:) = data(end,:)*yts;
|
---|
46 | end
|
---|
47 |
|
---|
48 | %Step 1: write the name to identify this record uniquely
|
---|
49 | fwrite(fid,numel(name),'int');
|
---|
50 | fwrite(fid,name,'char');
|
---|
51 |
|
---|
52 | %Step 2: write the data itself.
|
---|
53 | if strcmpi(format,'Boolean'),% {{{
|
---|
54 | if(numel(data)~=1), error(['field ' name ' cannot be marshalled as it has more than one element!']); end
|
---|
55 |
|
---|
56 | %first write length of record
|
---|
57 | fwrite(fid,4+4,'int'); %1 bool (disguised as an int)+code
|
---|
58 |
|
---|
59 | %write data code:
|
---|
60 | fwrite(fid,FormatToCode(format),'int');
|
---|
61 |
|
---|
62 | %now write integer
|
---|
63 | fwrite(fid,data,'int'); %send an int, not easy to send a bool
|
---|
64 | % }}}
|
---|
65 | elseif strcmpi(format,'Integer'), % {{{
|
---|
66 | if(numel(data)~=1), error(['field ' name ' cannot be marshalled as it has more than one element!']); end
|
---|
67 |
|
---|
68 | %first write length of record
|
---|
69 | fwrite(fid,4+4,'int'); %1 integer + code
|
---|
70 |
|
---|
71 | %write data code:
|
---|
72 | fwrite(fid,FormatToCode(format),'int');
|
---|
73 |
|
---|
74 | %now write integer
|
---|
75 | fwrite(fid,data,'int');
|
---|
76 | % }}}
|
---|
77 | elseif strcmpi(format,'Double'), % {{{
|
---|
78 | if(numel(data)~=1), error(['field ' name ' cannot be marshalled as it has more than one element!']); end
|
---|
79 |
|
---|
80 | %first write length of record
|
---|
81 | fwrite(fid,8+4,'int'); %1 double+code
|
---|
82 |
|
---|
83 | %write data code:
|
---|
84 | fwrite(fid,FormatToCode(format),'int');
|
---|
85 |
|
---|
86 | %now write double
|
---|
87 | fwrite(fid,data,'double');
|
---|
88 | % }}}
|
---|
89 | elseif strcmpi(format,'String'), % {{{
|
---|
90 | %first write length of record
|
---|
91 | fwrite(fid,length(data)+4+4,'int'); %string + string size + code
|
---|
92 |
|
---|
93 | %write data code:
|
---|
94 | fwrite(fid,FormatToCode(format),'int');
|
---|
95 |
|
---|
96 | %now write string
|
---|
97 | fwrite(fid,length(data),'int');
|
---|
98 | fwrite(fid,data,'char');
|
---|
99 | % }}}
|
---|
100 | elseif strcmpi(format,'BooleanMat'), % {{{
|
---|
101 |
|
---|
102 | %Get size
|
---|
103 | s=size(data);
|
---|
104 | %if matrix = NaN, then do not write anything
|
---|
105 | if (s(1)==1 & s(2)==1 & isnan(data)),
|
---|
106 | s(1)=0; s(2)=0;
|
---|
107 | end
|
---|
108 |
|
---|
109 | %first write length of record
|
---|
110 | fwrite(fid,4+4+8*s(1)*s(2)+4+4,'int'); %2 integers (32 bits) + the double matrix + code + matrix type
|
---|
111 |
|
---|
112 | %write data code and matrix type:
|
---|
113 | fwrite(fid,FormatToCode(format),'int');
|
---|
114 | fwrite(fid,mattype,'int');
|
---|
115 |
|
---|
116 | %now write matrix
|
---|
117 | fwrite(fid,s(1),'int');
|
---|
118 | fwrite(fid,s(2),'int');
|
---|
119 | if s(1)*s(2),
|
---|
120 | fwrite(fid,data','double'); %get to the "c" convention, hence the transpose
|
---|
121 | end
|
---|
122 | % }}}
|
---|
123 | elseif strcmpi(format,'IntMat'), % {{{
|
---|
124 |
|
---|
125 | %Get size
|
---|
126 | s=size(data);
|
---|
127 | %if matrix = NaN, then do not write anything
|
---|
128 | if (s(1)==1 & s(2)==1 & isnan(data)),
|
---|
129 | s(1)=0; s(2)=0;
|
---|
130 | end
|
---|
131 |
|
---|
132 | %first write length of record
|
---|
133 | fwrite(fid,4+4+8*s(1)*s(2)+4+4,'int'); %2 integers (32 bits) + the double matrix + code + matrix type
|
---|
134 |
|
---|
135 | %write data code and matrix type:
|
---|
136 | fwrite(fid,FormatToCode(format),'int');
|
---|
137 | fwrite(fid,mattype,'int');
|
---|
138 |
|
---|
139 | %now write matrix
|
---|
140 | fwrite(fid,s(1),'int');
|
---|
141 | fwrite(fid,s(2),'int');
|
---|
142 | if s(1)*s(2),
|
---|
143 | fwrite(fid,data','double'); %get to the "c" convention, hence the transpose
|
---|
144 | end
|
---|
145 | % }}}
|
---|
146 | elseif strcmpi(format,'DoubleMat'), % {{{
|
---|
147 |
|
---|
148 | %Get size
|
---|
149 | s=size(data);
|
---|
150 | %if matrix = NaN, then do not write anything
|
---|
151 | if (s(1)==1 & s(2)==1 & isnan(data)),
|
---|
152 | s(1)=0; s(2)=0;
|
---|
153 | end
|
---|
154 |
|
---|
155 | %first write length of record
|
---|
156 | recordlength=4+4+8*s(1)*s(2)+4+4; %2 integers (32 bits) + the double matrix + code + matrix type
|
---|
157 | if recordlength>2^31; error(['field ' name ' cannot be marshalled because it is larger than 2^31 bytes!']); end
|
---|
158 | fwrite(fid,recordlength,'int');
|
---|
159 |
|
---|
160 | %write data code and matrix type:
|
---|
161 | fwrite(fid,FormatToCode(format),'int');
|
---|
162 | fwrite(fid,mattype,'int');
|
---|
163 |
|
---|
164 | %now write matrix
|
---|
165 | fwrite(fid,s(1),'int');
|
---|
166 | fwrite(fid,s(2),'int');
|
---|
167 | if s(1)*s(2),
|
---|
168 | fwrite(fid,data','double'); %get to the "c" convention, hence the transpose
|
---|
169 | end
|
---|
170 | % }}}
|
---|
171 | elseif strcmpi(format,'CompressedMat'), % {{{
|
---|
172 |
|
---|
173 | %Get size
|
---|
174 | s=size(data);
|
---|
175 |
|
---|
176 | if (s(1)==1 & s(2)==1 & isnan(data)),
|
---|
177 | s(1)=0; s(2)=0;
|
---|
178 | end
|
---|
179 |
|
---|
180 | %first write length of record
|
---|
181 | recordlength=4+4+8+8+1*(s(1)-1)*s(2)+8*s(2)+4+4; %2 integers (32 bits) + the matrix + code + matrix type
|
---|
182 | if recordlength>2^31; error(['field ' name ' cannot be marshalled because it is larger than 2^31 bytes!']); end
|
---|
183 | fwrite(fid,recordlength,'int');
|
---|
184 |
|
---|
185 | %write data code and matrix type:
|
---|
186 | fwrite(fid,FormatToCode(format),'int');
|
---|
187 | fwrite(fid,mattype,'int');
|
---|
188 |
|
---|
189 | %write matrix size
|
---|
190 | fwrite(fid,s(1),'int');
|
---|
191 | fwrite(fid,s(2),'int');
|
---|
192 |
|
---|
193 | if s(1)*s(2),
|
---|
194 |
|
---|
195 | %Write offset and range
|
---|
196 | A = data(1:end-1,:);
|
---|
197 | offset = min(A(:));
|
---|
198 | range = max(A(:)) - offset;
|
---|
199 | fwrite(fid,offset,'double');
|
---|
200 | fwrite(fid,range,'double');
|
---|
201 |
|
---|
202 | %Convert data to uint8 and write it
|
---|
203 | A=uint8((A-offset)/range*255);
|
---|
204 | fwrite(fid,A','uint8'); %get to the "c" convention, hence the transpose
|
---|
205 |
|
---|
206 | %Write last row as double (time)
|
---|
207 | fwrite(fid,data(end,:),'double');
|
---|
208 | else
|
---|
209 |
|
---|
210 | %Write empty offset and range
|
---|
211 | fwrite(fid,0,'double');
|
---|
212 | fwrite(fid,0,'double');
|
---|
213 | end
|
---|
214 | % }}}
|
---|
215 | elseif strcmpi(format,'MatArray'), % {{{
|
---|
216 |
|
---|
217 | numrecords=numel(data);
|
---|
218 |
|
---|
219 | %first get length of record
|
---|
220 | recordlength=4+4; %number of records + code
|
---|
221 | for i=1:numrecords,
|
---|
222 | matrix=data{i};
|
---|
223 | s=size(matrix);
|
---|
224 | recordlength=recordlength+4*2+... %row and col of matrix
|
---|
225 | s(1)*s(2)*8; %matrix of doubles
|
---|
226 | end
|
---|
227 |
|
---|
228 | %write length of record
|
---|
229 | fwrite(fid,recordlength,'int');
|
---|
230 |
|
---|
231 | %write data code:
|
---|
232 | fwrite(fid,FormatToCode(format),'int');
|
---|
233 |
|
---|
234 | %write data, first number of records
|
---|
235 | fwrite(fid,numrecords,'int');
|
---|
236 |
|
---|
237 | %write each matrix:
|
---|
238 | for i=1:numrecords,
|
---|
239 | matrix=data{i};
|
---|
240 | s=size(matrix);
|
---|
241 | fwrite(fid,s(1),'int');
|
---|
242 | fwrite(fid,s(2),'int');
|
---|
243 | fwrite(fid,matrix','double');
|
---|
244 | end
|
---|
245 | % }}}
|
---|
246 | elseif strcmpi(format,'StringArray'), % {{{
|
---|
247 |
|
---|
248 | %first get length of string array:
|
---|
249 | num=numel(data);
|
---|
250 | if isnumeric(data) & num==1 & isnan(data),
|
---|
251 | num = 0;
|
---|
252 | end
|
---|
253 |
|
---|
254 | %now get length of record:
|
---|
255 | recordlength=4+4; %for length of array + code
|
---|
256 | for i=1:num,
|
---|
257 | string=data{i};
|
---|
258 | recordlength=recordlength+4+length(string); %for each string
|
---|
259 | end
|
---|
260 |
|
---|
261 | %write length of record
|
---|
262 | fwrite(fid,recordlength,'int');
|
---|
263 |
|
---|
264 | %write data code:
|
---|
265 | fwrite(fid,FormatToCode(format),'int');
|
---|
266 |
|
---|
267 | %now write length of string array
|
---|
268 | fwrite(fid,num,'int');
|
---|
269 |
|
---|
270 | %now write the strings
|
---|
271 | for i=1:num,
|
---|
272 | string=data{i};
|
---|
273 | fwrite(fid,length(string),'int');
|
---|
274 | fwrite(fid,string,'char');
|
---|
275 | end
|
---|
276 | % }}}
|
---|
277 | else % {{{
|
---|
278 | error(['WriteData error message: data type: ' num2str(format) ' not supported yet! (' name ')']);
|
---|
279 | end % }}}
|
---|
280 | end
|
---|
281 |
|
---|
282 | function code=FormatToCode(format) % {{{
|
---|
283 | %This routine takes the format string, and hardcodes it into an integer, which
|
---|
284 | %is passed along the record, in order to identify the nature of the dataset being
|
---|
285 | %sent.
|
---|
286 | if strcmpi(format,'Boolean'),
|
---|
287 | code=1;
|
---|
288 | elseif strcmpi(format,'Integer'),
|
---|
289 | code=2;
|
---|
290 | elseif strcmpi(format,'Double'),
|
---|
291 | code=3;
|
---|
292 | elseif strcmpi(format,'String'),
|
---|
293 | code=4;
|
---|
294 | elseif strcmpi(format,'BooleanMat'),
|
---|
295 | code=5;
|
---|
296 | elseif strcmpi(format,'IntMat'),
|
---|
297 | code=6;
|
---|
298 | elseif strcmpi(format,'DoubleMat'),
|
---|
299 | code=7;
|
---|
300 | elseif strcmpi(format,'MatArray'),
|
---|
301 | code=8;
|
---|
302 | elseif strcmpi(format,'StringArray'),
|
---|
303 | code=9;
|
---|
304 | elseif strcmpi(format,'CompressedMat'),
|
---|
305 | code=10;
|
---|
306 | else
|
---|
307 | error('FormatToCode error message: data type not supported yet!');
|
---|
308 | end
|
---|
309 | end% }}}
|
---|