File: loadmatfile.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 (225 lines) | stat: -rw-r--r-- 6,004 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
function loadmatfile(varargin)
// Loads 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)

fil=[]
bin=[]
varnames=[]

if rhs==1 then // Compatibility with old loadmatfile version
  bin=%T
  fil=varargin(1)
else // Try to find type binary or ASCII ?
  // Filename is the first parameter: loadmatfile(filename[,opts])
  // or the second parameter: loadmatfile(filetype,filename[,opts]) with filetype equal to -ascii or -mat
  // filetype can also be included in opts
  k=1
  while k<=lstsize(varargin)
    select varargin(k)
    case "-mat"
      bin=%T
      k=k+1
    case "-ascii"
      bin=%F
      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(fil) then // Filename
	fil=varargin(k)
	if fileparts(fil,"extension")==".mat" & isempty(bin) then // extension .mat and bin not already fixed by options
	  bin=%T
	elseif isempty(bin) then
	  bin=%F
	end
      else // Variable names
	varnames=[varnames;varargin(k)]
      end
      k=k+1
    end
  end
  if isempty(bin) then
    bin=%T
  end
end

if fileparts(fil,"extension")=="" then
  fil=fil+".mat"
end

// --- BINARY FILE ---
if bin then 
  ReadmiMatrix=ReadmiMatrix; // Loads the matfile functions (ReadmiMatrix.sci) 

  // Try to read file as a level 5 binary file
  // File opening
  fd=open_matfile(fil)
  // Reads the file header
  ierr=execstr("[head,version,endian]=matfile_header(fd)","errcatch")
  
  if ierr<>0 then
    errmsg=lasterror()
    // This line has to be mofified according to error message in 'matfile_header' function
    if stripblanks(errmsg)=="Invalid level 5 binary MAT-file!" then 
      // Level 4 binary file ?
      level=4;
    else
      // Wrong file
      error(errmsg);
    end
  else
    // Level 5 binary file
    level=5;
  end
  
  // --- LEVEL 5 BINARY FILE (This part already existed in old version) ---
  if level==5 then
    //--set constants
    exec(LoadMatConstants,-1);
    
    //--loop on the stored variables
    Name='';Names=[];Matrices=list()
    while %t 
      ierr=execstr('[Matrix,Name]=ReadmiMatrix(fd)','errcatch') //read next variable
      if ierr<>0 then mclose(fd),disp(lasterror()),return,end 
      if meof(fd) then  break,end //EOF reached 
      
      // Old version compatibility | Name has been given
      if isempty(varnames) | or(Name==varnames) then
	Names=[Names,Name];Matrices($+1)=Matrix
      end
      
    end
    //--file closing
    mclose(fd)
    
    //return variables in the calling context
    execstr('['+strcat(Names,',')+']=resume(Matrices(:))')
  
  // --- LEVEL 4 BINARY FILE (Copy/Paste from mtlb_load.sci) ---
  elseif level==4 then
    l_flags=['dl','fl','ll','sl','uls','uc']
    b_flags=['db','fb','lb','sb','ubs','uc']
    deff('Error(msg)',['mclose(fd)' ;'error(msg)'])
    [fd,err]=mopen(fil,'rb',0)
    if err<>0 then error('File '+fil+' cannot be opened for reading'),end
    
    vars=list() //list to store loaded variables
    names=[]  // vector of variables names
    
    while %t 
      offset=mtell(fd)
      mopt=mget(1,'uil',fd)
      if meof(fd)<>0 then break,end
      if mopt>5000 then
	mseek(offset,fd)
	mopt=mget(1,'uib',fd)
	
	if mopt>5000 then
	  Error('Incorrect file')
	end
      end
      MOPT=[]
      for k=1:4
	r=mopt-10*int(mopt/10);
	mopt=int((mopt-r)/10);
	MOPT=[r MOPT];
      end
      select MOPT(1)
      case 0
	fl='uil'
	flag=l_flags(MOPT(3)+1)
      case 1
	fl='uib'
	flag=b_flags(MOPT(3)+1)
      case 2
	Error('VAX D-float not handled')
      case 3
	Error('VAX G-float not handled')
      case 4
	Error('Cray encoding not handled')
      else
	Error('Unknown binary number format')
      end
      t=mget(4,fl,fd);
      if meof(fd)<>0 then Error('Incorrect file'),end
      m=t(1);n=t(2);it=t(3),namelen=t(4)
      name=mget(namelen,"c",fd);
      if meof(fd)<>0 then Error('Incorrect file'),end
      name=ascii(name(1:namelen-1))
      
      // Old version compatibility | Name has been given
      if isempty(varnames) | or(name==varnames) then
	names=[names name]
      end
      
      if MOPT(4)==0 then  // regular matrix
	v=mget((it+1)*m*n,flag,fd);
	if meof(fd)<>0 then Error('Incorrect file'),end
	if it==0 then
	  mat=matrix(v,m,n);
	elseif it==1
	  mat=matrix(v(1:m*n),m,n)+%i*matrix(v(m*n+1:$),m,n)
	end
      elseif MOPT(4)==1 // vector of strings
	v=mget(m*n,flag,fd);
	if meof(fd)<>0 then Error('Incorrect file'),end
	mat=matrix(v(1:m*n),m,n);
	w=mat;
	mat=[];
	for k=1:m
	  mat=[mat;ascii(w(k,:))]
	end
      elseif MOPT(4)==2 //sparse matrix
	//sparse
	Nnz=m-1;
	it=n-3;if it<>0&it<>1 then Error('Unknown sparse type'),end
	ir=mget(Nnz,flag,fd);m=mget(1,"d",fd);
	jc=mget(Nnz,flag,fd);n=mget(1,"d",fd);
	v=mget(Nnz,flag,fd);junk=mget(1,"d",fd);
	if meof(fd)<>0 then Error('Incorrect file'),end
	if it==1 then
	  //complex
	  v=v+%i*mget(Nnz,flag,fd);
	end
	mat=sparse([ir;jc]',v,[m n]);
      end
      // Old version compatibility | Name has been given
      if isempty(varnames) | or(name==varnames) then
	vars($+1)=mat
      end
    end
    mclose(fd);
    //form execstr instruction to resume variables in the calling environment
    execstr('['+strcat(names,',')+']=resume(vars(:))')  
  else
    error("Unknown Matlab binary file format");
  end
   
// --- ASCII FILE (Copy/Paste from mtlb_load.sci) ---
else
  ke=strindex(fil,'.')
  if ke==[] then ke=length(fil),else ke=ke($)-1,end
  kp=strindex(fil,['/','\'])
  if kp==[] then kp=1,else kp=kp($)+1,end
  name=part(fil,kp:ke)
  mat=evstr(mgetl(fil))
  execstr(name+'= resume(mat)')
end
endfunction