File: WritemiMatrix.sci

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (389 lines) | stat: -rw-r--r-- 8,878 bytes parent folder | download | duplicates (2)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
function WritemiMatrix(fd,value,ArrayName)
// Save variables in a Matlab binary file
// This function has been developped following the 'MAT-File Format' description:
// www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf 
// Copyright INRIA
// Authors: VC

TagSize=8; // 8 bytes
ArrayFlagSize=8; // 8 bytes

// Position is saved to come back here after writing
SavePosBefore=mtell(fd);

// Save space for TAG
WriteEmptyTag(fd);

// Position is saved to compute number of written bytes
NumberOfBytes=mtell(fd);

// Save space for ARRAY FLAGS
WriteEmptyArrayFlags(fd);

// Compute array dimensions
if type(value)==10 then
  WriteDimensionArray(fd,size(mstr2sci(value)));
else
  WriteDimensionArray(fd,size(value));
end

// Write variable name
WriteArrayName(fd,ArrayName);

Flags=[0 0 0];

if type(value)==1 then // DOUBLE value
  value=matrix(value,1,-1);
  Flags(1)=bool2s(~isreal(value));
  Class=DoubleClass;
  NnzMax=0;
  WriteSimpleElement(fd,real(value),miDOUBLE);
  if Flags(1) then
    WriteSimpleElement(fd,imag(value),miDOUBLE);
  end
elseif type(value)==10 then // CHARACTER STRING value
  if size(value,"*")==1 then
    value=matrix(ascii(mstr2sci(value)),1,-1);
    Flags(1)=0;
    Class=CharClass;
    NnzMax=0;
    WriteSimpleElement(fd,value,miUINT16);
  else
    warning("Scilab string matrix saved as Matlab Cell");
    sz=size(value);
    value=matrix(value,1,-1);
    entries=list()
    for k=1:size(value,2)
      entries(k)=value(k);
    end
    value=mlist(["ce","dims","entries"],int32(sz),entries)
    mseek(SavePosBefore,fd);
    WritemiMatrix(fd,value,ArrayName);
    return
  end
elseif type(value)==8 then // INTEGER value
  value=matrix(value,1,-1);
  Flags(1)=0;
  NnzMax=0;
  select typeof(value)
  case "int8"
    Class=Int8Class;
    WriteSimpleElement(fd,value,miINT8);
  case "uint8"
    Class=Uint8Class;
    WriteSimpleElement(fd,value,miUINT8);
  case "int16"
    Class=Int16Class;
    WriteSimpleElement(fd,value,miINT16);
  case "uint16"
    Class=Uint16Class;
    WriteSimpleElement(fd,value,miUINT16);
  case "int32"
    Class=Int32Class;
    WriteSimpleElement(fd,value,miINT32);
  case "uint32"
    Class=Uint32Class;
    WriteSimpleElement(fd,value,miUINT32);
  else
    error("Unknown integer type: "+typeof(value))
  end
elseif type(value)==17 then // MLIST used ofr CELLS and STRUCTS
  Flags(1)=0;
  NnzMax=0;
  select typeof(value)
  case "ce" // CELL
    Class=CellClass;
    for k=1:lstsize(value.entries)
      WritemiMatrix(fd,value(k).entries,"");
    end
  case "st" // STRUCT
    Class=StructClass;
    Fnams=getfield(1,value);
    Fnams(1:2)=[];
    FieldNameLength=32;
    WriteSimpleElement(fd,FieldNameLength,miINT32);
    
    NumberOfFields=size(Fnams,2);
    FieldNames=[]
    for k=1:NumberOfFields
      FieldNames=[FieldNames ascii(Fnams(k)) zeros(1,FieldNameLength-length(Fnams(k)))];
    end
    
    WriteSimpleElement(fd,FieldNames,miINT8);
    if prod(size(value))==1 then
      for k=1:NumberOfFields
	WritemiMatrix(fd,value(Fnams(k)),"");
      end
    else
      for i=1:prod(size(value))
	for k=1:NumberOfFields
	  WritemiMatrix(fd,value(i)(Fnams(k)),"");
	end
      end
    end
  else
    error(typeof(value)+" mlist type not yet implemented");
  end
elseif or(type(value)==[5,7]) then // SPARSE matrices
  if type(value)==5 then // Scilab sparse is converted to Matlab sparse
    value=mtlb_sparse(value);
  end
  Class=SparseClass;
  [ij,v,mn]=spget(value);
  RowIndex=ij(:,1)-1;
  col=ij(:,2);
  NnzMax=length(RowIndex);
  
  WriteSimpleElement(fd,RowIndex,miINT32);
  
  ColumnIndex=col(1);
  for k=1:size(col,"*")-1
    if col(k)<>col(k+1) then
      ColumnIndex=[ColumnIndex;col(k+1)]
    end
  end
  
  ptr=0;
  for k=1:size(ColumnIndex,"*")
    ptr=[ptr;size(find(col==ColumnIndex(k)),"*")]
  end
  ColumnIndex=cumsum(ptr);
  
  WriteSimpleElement(fd,ColumnIndex,miINT32);
  
  Flags(1)=bool2s(~isreal(v));
  WriteSimpleElement(fd,real(v),miDOUBLE);
  if Flags(1) then
    WriteSimpleElement(fd,imag(v),miDOUBLE);
  end
else
  error(typeof(value)+" not yet implemented");
end

SavePosAfter=mtell(fd);

NumberOfBytes=SavePosAfter-NumberOfBytes

// Update tag
WriteTag(fd,miMatrix,NumberOfBytes);

mseek(SavePosBefore+TagSize+TagSize+ArrayFlagSize,fd);

// Update array flags
WriteArrayFlags(fd,Flags,Class,NnzMax);

mseek(SavePosAfter,fd);
endfunction

function fd=open_matfile_wb(fil)
// Copyright INRIA
// Opens a file in 'w+b' mode
// VC
fil=stripblanks(fil)
fd=mopen(fil,"w+b",0)
endfunction

function swap=write_matfile_header(fd)
// Copyright INRIA
// Write the mat file header informations
// VC

head="MATLAB 5.0 MAT-file, Generated by Scilab"
head=head+part(" ",1:(124-length(head)));
mput(ascii(head),'uc',fd);

version=[1 0];
mput(version,'uc',fd);

endian_indicator=ascii(["M" "I"]);
mput(endian_indicator,'uc',fd);

// Character are read just after to get endian
// Because mput swap automatically bytes 
// if endian not given when writing
mseek(mtell(fd)-2,fd);
IM_MI=mget(2,'uc',fd);
if and(IM_MI==[73,77]) then // little endian file
  swap='l'
elseif and(IM_MI==[77,73]) then // big endian file
  swap='b'
else
  error("Error while writing MI");
end
// Following call to mseek is needed under Windows
// to set file pointer after reading
mseek(0,fd,'cur');
endfunction

function WriteEmptyTag(fd)
// Copyright INRIA
// Reserve space for a tag
// VC

for k=1:TagSize
  mput(0,'uc',fd);
end
endfunction

function WriteEmptyArrayFlags(fd)
// Copyright INRIA
// Reserve space for an array flag
// VC

for k=1:ArrayFlagSize+TagSize
  mput(0,'uc',fd);
end
endfunction

function WriteArrayFlags(fd,Flags,Class,NnzMax)
// Copyright INRIA
// Write an array flag
// VC

WriteTag(fd,miUINT32,ArrayFlagSize);

mseek(mtell(fd)-ArrayFlagSize,fd);

Flags=[0 Flags(3:-1:1)];

B=[0 0 0 0];
B(3)=bits2byte(Flags);
B(4)=Class;
mput(B,"uc",fd);

mput(NnzMax,md_i,fd);
endfunction

function WriteDimensionArray(fd,dims)
// Copyright INRIA
// Write dimensions of an array
// VC

WriteSimpleElement(fd,dims,miINT32);
endfunction

function WriteArrayName(fd,ArrayName)
// Copyright INRIA
// Write name of an array
// VC

WriteSimpleElement(fd,ascii(ArrayName),miINT8);
endfunction

function WriteTag(fd,DataType,NumberOfBytes,Compressed)
// Copyright INRIA
// Write a tag
// VC

SavePos=mtell(fd);

if argn(2)==3 then
  Compressed=%F;
end
Compressed=NumberOfBytes<=4;

if Compressed then
  mseek(SavePos-NumberOfBytes-TagSize/2,fd);
  mput(NumberOfBytes,md_s,fd);
  mput(DataType,md_s,fd);
else
  mseek(SavePos-NumberOfBytes-TagSize,fd);
  mput(DataType,md_i,fd);
  mput(NumberOfBytes,md_i,fd);
end

mseek(SavePos,fd);
endfunction

function WriteSimpleElement(fd,value,DataType)
// Copyright INRIA
// Write an element in file
// VC

// If data is of double type
// and made of integer values 
// then it is writen in an INT* format to save space
if DataType==miDOUBLE & and(double(int(value))==value) then
  if min(value)>=0 & max(value)<=255 then // min and max value for int8
    DataType=miUINT8;
  elseif min(value)>=-128 & max(value)<=127 then // min and max value for int8
    DataType=miINT8;
    //miINT8 replaced by miINT16 due to an error somewhere (matlab or
    //scilab?) the generated file gives incorrect result in Matlab!
    //example:
    //  scilab var=-40;savematfile('foosci.mat','var','-mat','-v6');
    //  matlab load foosci.mat;var
    DataType=miINT16;

  elseif min(value)>=0 & max(value)<=65535 then // min and max value for int16
    DataType=miUINT16;
  elseif min(value)>=-32768 & max(value)<=32767 then // min and max value for int16
    DataType=miINT16;
  elseif min(value)>=0 & max(value)<=4294967295 then // min and max value for int32
    DataType=miINT32;
  elseif min(value)>=-2147483648 & max(value)<=2147483647 then // min and max value for int32
    DataType=miINT32;
  end
end

NumberOfValues=length(value);

WriteEmptyTag(fd);

select DataType
case miDOUBLE
  NumberOfBytes=NumberOfValues*8;
  fmt=md_d;
case miINT8
  NumberOfBytes=NumberOfValues;
  fmt="c";
case miUINT8
  NumberOfBytes=NumberOfValues;
  fmt="uc";
case miINT16
  NumberOfBytes=NumberOfValues*2;
  fmt=md_s;
case miUINT16
  NumberOfBytes=NumberOfValues*2;
  fmt="u"+md_s;
case miINT32
  NumberOfBytes=NumberOfValues*4;
  fmt=md_i;
case miUINT32
  NumberOfBytes=NumberOfValues*4;
  fmt="u"+md_i;
else
  error("DataType: "+string(DataType)+"is not implemented");
end

Compressed=NumberOfBytes<=4;
if Compressed then
  mseek(mtell(fd)-TagSize/2,fd);
end

mput(value,fmt,fd);

WriteTag(fd,DataType,NumberOfBytes);

WritePaddingBytes(fd);

endfunction

function WritePaddingBytes(fd)
// Copyright INRIA
// Write padding bytes to have a number of bytes multiple of 8
// VC

np=modulo(8-modulo(mtell(fd),8),8);
for k=1:np
  mput(0,"uc",fd);
end
endfunction

function i=bits2byte(b)
// Copyright INRIA
// Converts 4-bits value to a byte value
// VC

i=b* 2^(0:3)';
endfunction