~ubuntu-branches/ubuntu/karmic/scilab/karmic

« back to all changes in this revision

Viewing changes to macros/mtlb/mtlb_load.sci

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2002-03-21 16:57:43 UTC
  • Revision ID: james.westby@ubuntu.com-20020321165743-e9mv12c1tb1plztg
Tags: upstream-2.6
ImportĀ upstreamĀ versionĀ 2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function mtlb_load(thefile,opt)
 
2
//loads matlab 4.x  binary (.mat) or ascii files
 
3
//
 
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)'])
 
7
[lhs,rhs]=argn(0)
 
8
// look for file type
 
9
if rhs==2 then
 
10
  if convstr(opt)=='-ascii' then
 
11
    bin=%f
 
12
  else
 
13
    error(opt+' : Unknown option')
 
14
  end
 
15
else
 
16
  k=strindex(thefile,'.')
 
17
  if k==[] then  //no extension given
 
18
    thefile=thefile+'.mat'
 
19
    bin=%t
 
20
  else
 
21
    if part(thefile,k($):length(thefile))=='.mat' then 
 
22
      bin=%t,
 
23
    else
 
24
      bin=%f
 
25
    end
 
26
  end
 
27
end
 
28
 
 
29
if bin then
 
30
  [fd,err]=mopen(thefile,'rb',0)
 
31
  if err<>0 then error('File '+thefile+' cannot be opened for reading'),end
 
32
 
 
33
  vars=list() //list to store loaded variables
 
34
  names=[]  // vector of variables names
 
35
 
 
36
 
 
37
  while %t 
 
38
    offset=mtell(fd)
 
39
    mopt=mget(1,'uil',fd)
 
40
    if meof(fd)<>0 then break,end
 
41
    if mopt>5000 then
 
42
      mseek(offset,fd)
 
43
      mopt=mget(1,'uib',fd)
 
44
 
 
45
      if mopt>5000 then
 
46
        Error('Incorrect file')
 
47
      end
 
48
    end
 
49
    MOPT=[]
 
50
    for k=1:4
 
51
      r=mopt-10*int(mopt/10);
 
52
      mopt=int((mopt-r)/10);
 
53
      MOPT=[r MOPT];
 
54
    end
 
55
    select MOPT(1)
 
56
    case 0
 
57
      fl='uil'
 
58
      flag=l_flags(MOPT(3)+1)
 
59
    case 1
 
60
      fl='uib'
 
61
      flag=b_flags(MOPT(3)+1)
 
62
    case 2
 
63
      Error('VAX D-float not handled')
 
64
    case 3
 
65
      Error('VAX G-float not handled')
 
66
    case 4
 
67
      Error('Cray encoding not handled')
 
68
    else
 
69
      Error('Unknown binary number format')
 
70
    end
 
71
    t=mget(4,fl,fd);
 
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))
 
77
    names=[names name]
 
78
    
 
79
    
 
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
 
83
      if it==0 then
 
84
        mat=matrix(v,m,n);
 
85
      elseif it==1
 
86
        mat=matrix(v(1:m*n),m,n)+%i*matrix(v(m*n+1:$),m,n)
 
87
      end
 
88
    elseif MOPT(4)==1 // vector of strings
 
89
      v=mget(m*n,flag,fd);
 
90
      if meof(fd)<>0 then Error('Incorrect file'),end
 
91
      mat=matrix(v(1:m*n),m,n);
 
92
      w=mat;
 
93
      mat=[];
 
94
      for k=1:m
 
95
        mat=[mat;ascii(w(k,:))]
 
96
      end
 
97
    elseif MOPT(4)==2 //sparse matrix
 
98
      //sparse
 
99
      Nnz=m-1;
 
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
 
105
      if it==1 then
 
106
        //complex
 
107
        v=v+%i*mget(Nnz,flag,fd);
 
108
      end
 
109
      mat=sparse([ir;jc]',v,[m n]);
 
110
    end
 
111
    vars($+1)=mat
 
112
  end
 
113
  mclose(fd);
 
114
  //form execstr instruction to resume variables in the calling environment
 
115
  execstr('['+strcat(names,',')+']=resume(vars(:))')
 
116
else
 
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)')
 
124
end
 
125