1
function mtlb_load(thefile,opt)
2
//loads matlab 4.x binary (.mat) or ascii files
4
l_flags=['dl','fl','ll','sl','uls','uc']
5
b_flags=['db','fb','lb','sb','ubs','uc']
6
deff('Error(msg)',['mclose(fd)' ;'error(msg)'])
10
if convstr(opt)=='-ascii' then
13
error(opt+' : Unknown option')
16
k=strindex(thefile,'.')
17
if k==[] then //no extension given
18
thefile=thefile+'.mat'
21
if part(thefile,k($):length(thefile))=='.mat' then
30
[fd,err]=mopen(thefile,'rb',0)
31
if err<>0 then error('File '+thefile+' cannot be opened for reading'),end
33
vars=list() //list to store loaded variables
34
names=[] // vector of variables names
40
if meof(fd)<>0 then break,end
46
Error('Incorrect file')
51
r=mopt-10*int(mopt/10);
52
mopt=int((mopt-r)/10);
58
flag=l_flags(MOPT(3)+1)
61
flag=b_flags(MOPT(3)+1)
63
Error('VAX D-float not handled')
65
Error('VAX G-float not handled')
67
Error('Cray encoding not handled')
69
Error('Unknown binary number format')
72
if meof(fd)<>0 then Error('Incorrect file'),end
73
m=t(1);n=t(2);it=t(3),namelen=t(4)
74
name=mget(namelen,"c",fd);
75
if meof(fd)<>0 then Error('Incorrect file'),end
76
name=ascii(name(1:namelen-1))
80
if MOPT(4)==0 then // regular matrix
81
v=mget((it+1)*m*n,flag,fd);
82
if meof(fd)<>0 then Error('Incorrect file'),end
86
mat=matrix(v(1:m*n),m,n)+%i*matrix(v(m*n+1:$),m,n)
88
elseif MOPT(4)==1 // vector of strings
90
if meof(fd)<>0 then Error('Incorrect file'),end
91
mat=matrix(v(1:m*n),m,n);
95
mat=[mat;ascii(w(k,:))]
97
elseif MOPT(4)==2 //sparse matrix
100
it=n-3;if it<>0&it<>1 then Error('Unknown sparse type'),end
101
ir=mget(Nnz,flag,fd);m=mget(1,"d",fd);
102
jc=mget(Nnz,flag,fd);n=mget(1,"d",fd);
103
v=mget(Nnz,flag,fd);junk=mget(1,"d",fd);
104
if meof(fd)<>0 then Error('Incorrect file'),end
107
v=v+%i*mget(Nnz,flag,fd);
109
mat=sparse([ir;jc]',v,[m n]);
114
//form execstr instruction to resume variables in the calling environment
115
execstr('['+strcat(names,',')+']=resume(vars(:))')
117
ke=strindex(thefile,'.')
118
if ke==[] then ke=length(thefile),else ke=ke($)-1,end
119
kp=strindex(thefile,['/','\'])
120
if kp==[] then kp=1,else kp=kp($)+1,end
121
name=part(thefile,kp:ke)
122
mat=evstr(mgetl(thefile))
123
execstr(name+'= resume(mat)')