File: savematfile.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 (333 lines) | stat: -rw-r--r-- 8,504 bytes parent folder | download
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
function savematfile(varargin)
// Save variables in a Matlab binary or ASCII file into Scilab
// 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: SS, VC

// Verify that all inputs are character strings
for k=1:size(varargin)
  if type(varargin(k))<>10 then
    error("All inputs must be character strings");
  end
end

[lhs,rhs]=argn(0);

mtlb_opts=[]; // Options for ASCII format
mtlb_thefile=[]; // Name of file to write
mtlb_names=[]; // Variable names to save
version=[]; // MAT-file version (4 or 6, miCOMPRESSED not yet implemented)
bin=[]; // %T is binary file %F if ASCII file

// Default format is binary
if rhs==1 then
  bin=%T;
end

// Sort all inputs (Options/Names/Filename)
k=1
while k<=lstsize(varargin)
  // All options are converted to lower case
  if part(varargin(k),1)=="-" then
    varargin(k)=convstr(varargin(k));
  end
  
  select varargin(k)
  case "-append"
    warning("Option -append not implemented: IGNORED")
    k=k+1
  case "-mat"
    bin=%T
    k=k+1
  case "-ascii"
    mtlb_opts=[mtlb_opts varargin(k)];
    bin=%F
    k=k+1
  case "-struct"
    k=k+1;
    stname=varargin(k);
    k=k+1;
    // Verify if one or more field name is/are given
    if k<=lstsize(varargin) & part(varargin(k),1)<>"-" & mtlb_thefile<>"" then // struct field
      while k<=lstsize(varargin) & part(varargin(k),1)<>"-"
	// Add field to variable names
	mtlb_names=[mtlb_names;varargin(k)]; 
	execstr(varargin(k)+"="+stname+"(mtlb_names($))");
	k=k+1;
      end
    else // All fields have to be saved
      fields=getfield(1,evstr(stname));
      fields(1:2)=[]
      for kk=fields
	mtlb_names=[mtlb_names;kk];
	execstr(kk+"="+stname+"(mtlb_names($))");
      end
    end
  case "-v4"
    version=4;
    bin=%T;
    k=k+1
  case "-v6"
    version=6;
    bin=%T;
    k=k+1
  case "-tabs"
    bin=%F;
    mtlb_opts=[mtlb_opts varargin(k)];
    k=k+1
  case "-double"
    bin=%F;
    mtlb_opts=[mtlb_opts varargin(k)];
    k=k+1
  case "-regexp"
    warning("Option -regexp not implemented: IGNORED")
    while k<=lstsize(varargin) & and(varargin(k)<>["-mat","-ascii"])
      k=k+1
    end
  else 
    if isempty(mtlb_thefile) then // Filename
      mtlb_thefile=varargin(k)
      if fileparts(mtlb_thefile,"extension")==".mat" & isempty(bin) then // extension .mat and bin not already fixed by options
	bin=%T
      end
    else // Variable names
      mtlb_names=[mtlb_names;varargin(k)]
    end
    k=k+1
  end
end

// Default version 6 for binary files
if isempty(version) & bin then
  version=6;
  warning("Option -v6 added");
end

// If no name given then all workspace saved
if isempty(mtlb_names) then
  mtlb_names=who("get");
  
  // Part to delete Scilab variables from mtlb_names (should be improved)
  mtlb_names(1:10)=[];// clear this function variables
  mtlb_names(($-predef()+1):$)=[]; // clear predefined variables
end

// If binary format and no extension for filename, .mat is added
if bin & isempty(strindex(mtlb_thefile,".")) then
  mtlb_thefile=mtlb_thefile+".mat"
end

// Do not handle function redefinition
funcprot(0);

// Binary save
if bin then
  // LEVEL 4 MAT-file (This part comes from mtlb_save.sci)
  if version==4 then
    // Matlab 5 types are not saved (structs...)
    for k=size(mtlb_names,"*"):-1:1
      execstr("x="+mtlb_names(k))
      if and(type(x)<>[1 4 5 6 10]) then
	warning("Variable "+mtlb_names(k)+" can not be save in level 4 MAT-file: IGNORED");
	mtlb_names(k)=[]
      end
    end
    
    // Open file for writing
    [mtlb_fd,err]=mopen(mtlb_thefile,"wb",0)

    // Clear variable wich are no more used to avoid name conflicts
    for k=["varargin","mtlb_names","mtlb_fmt","mtlb_fd"]
      if or(mtlb_names==k) then
	error("Name conflict: it is not possible to save variable with name "+k)
      end
    end
    clear("x","k","rhs","lhs","kk","err","bin","version","mtlb_thefile","mtlb_opts");
    
    // Following 'for loop' from SS
    for mtlb_k=1:size(mtlb_names,"*")
      // perform changes on variables
      execstr("x="+mtlb_names(mtlb_k))
      it=0
      select type(x)
      case 1 then
	P=0
	T=0
	if norm(imag(x),1)<>0 then it=1,end
      case 4 then
	x=bool2s(x)
	P=5
	T=0
      case 5 then
	if norm(imag(x),1)<>0 then it1=1,else it1=0,end
	P=0
	T=2
	[x,v,mn]=spget(x);
	if it1==0 then
	  x=[x real(v);[mn 0]]
	else
	  x=[x real(v) imag(v);[mn 0 0]]
	end
      case 6 then
	x=bool2s(x)
	P=0
	T=2
	[x,v,mn]=spget(x);
	x=[x v;[mn 0]]
      case 8 then
	T=0
	select inttype(x)
	case 4 then P=2,
	case 14 then P=2,
	case 2 then P=3
	case 12 then P=4
	case 1 then P=5,
	case 11 then P=5,
	end
	x=double(x)
      case 10 then
	x1=part(x(:),1:max(length(x)))
	x=[]
	for l=1:size(x1,1)
	  x=[x;ascii(x1(l))]
	end
	P=5
	T=1
      else
	error("Attempt to write an unsupported data type to an ASCII file")
      end
      [m,n]=size(x)
      
      
      M = 0 //little endian
      O = 0
      MOPT=[M O P T]
      
      [m,n]=size(x)
      head=[MOPT*[1000;100;10;1] m,n,it,length(mtlb_names(mtlb_k))+1]
      
      head=mput(head,"uil",mtlb_fd);
      mput([ascii(mtlb_names(mtlb_k)) 0],"c",mtlb_fd);
      select P
      case 0 then
	flag="dl"
      case 1 then
	flag="fl"
      case 2 then
	flag="il"
      case 3 then
	flag="sl"
      case 4 then
	flag="usl"
      case 5 then
	flag="uc"
      end
      if T==0 then
	if x<>[] then
	  mput(real(x(:).'),flag,mtlb_fd);
	  if it==1
	    mput(imag(x(:).'),flag,mtlb_fd);
	  end
	end
      elseif T==1
	v=mput(x(:).',flag,mtlb_fd);
      elseif T==2 then  //sparse
	mput(x(:).',flag,mtlb_fd);
      end
    end
    mclose(mtlb_fd);
    // End of loop written by SS
  // LEVEL 6 MAT-file  
  elseif version==6 then
    // Load functions
    ReadmiMatrix=ReadmiMatrix;
    WritemiMatrix=WritemiMatrix;
    
    // Open file for writing
    mtlb_fd=open_matfile_wb(mtlb_thefile);
    
    // Write header
    endian=write_matfile_header(mtlb_fd);
  
    //--set constants
    exec(LoadMatConstants,-1);
    
    // Clear variable wich are no more used to avoid name conflicts
    for k=["endian","varargin","mtlb_names","mtlb_fmt","mtlb_fd"]
      if or(mtlb_names==k) then
	error("Name conflict: it is not possible to save variable with name "+k)
      end
    end
    clear("x","k","rhs","lhs","kk","err","sep","bin","version","mtlb_thefile","mtlb_opts");

    // Write variables as miMATRIX data type
    for k=1:size(mtlb_names,"*")
      WritemiMatrix(mtlb_fd,evstr(mtlb_names(k)),mtlb_names(k));
    end
    
    mclose(mtlb_fd);
  else
    // This part should contain miCOMPRESSED data type handling
    error("Version "+string(version)+" MAT-file not implemented");
  end
  
// ASCII save
else
  // The end of this function has been adapted from mtlb_save.sci 

  // Matlab 5 types are not saved (structs...)
  for k=size(mtlb_names,"*"):-1:1
    execstr("x="+mtlb_names(k))
    if and(type(x)<>[1 4 5 6 10]) then
      warning("Variable "+mtlb_names(k)+" can not be save in ASCII file: IGNORED");
      mtlb_names(k)=[]
    end
  end
  if ( (mtlb_opts <> []) & (strindex("-tabs",mtlb_opts)<>[]) ) then
    sep=code2str(-40)
  else
    sep=" "
  end
  if size(mtlb_opts,"*")==1 then //8 digits save
    mtlb_fmt="(1pe14.7"+sep+")"
  else
    mtlb_fmt="(1pe23.15"+sep+")"
  end

  mtlb_fd=file("open",mtlb_thefile,"unknown")
  
  // Clear variable wich are no more used to avoid name conflicts
  for k=["varargin","mtlb_names","mtlb_fmt","mtlb_fd"]
    if or(mtlb_names==k) then
      error("Name conflict: it is not possible to save variable with name "+k)
    end
  end
  clear("x","k","rhs","lhs","kk","err","sep","bin","version","mtlb_thefile","mtlb_opts");

  for mtlb_k=1:size(mtlb_names,"*")
    // perform changes on variables
    execstr("x="+mtlb_names(mtlb_k))
    select type(x)
    case 1 then
      write(mtlb_fd,real(x),"("+string(size(x,2))+mtlb_fmt+")")
    case 4 then
      write(mtlb_fd,bool2s(x),"("+string(size(x,2))+mtlb_fmt+")")
    case 5 then
      [ij,x]=spget(real(x));x=[ij x];
      write(mtlb_fd,real(x),"(2f8.0,1x"+string(size(x,2))+mtlb_fmt+")")
    case 6 then
      [ij,x]=spget(bool2s(x));x=[ij x];
      write(mtlb_fd,real(x),"(2f8.0,1x"+string(size(x,2))+mtlb_fmt+")")
    case 10 then
      x=part(x(:),1:max(length(x)))
      x1=[]
      for l=1:size(x,1)
	x1=[x1;ascii(x(l))]
      end
      write(mtlb_fd,x1,"("+string(size(x1,2))+mtlb_fmt+")")
    end
  end
  file("close",mtlb_fd)
end
endfunction