File: loadmatfile.sci

package info (click to toggle)
scilab 5.2.2-9
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 334,832 kB
  • ctags: 52,586
  • sloc: xml: 526,945; ansic: 223,590; fortran: 163,080; java: 56,934; cpp: 33,840; tcl: 27,936; sh: 20,397; makefile: 9,908; ml: 9,451; perl: 1,323; cs: 614; lisp: 30
file content (131 lines) | stat: -rw-r--r-- 3,524 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
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2002-2004 - INRIA - Vincent COUVERT 
// Copyright (C) ???? - INRIA - Serge STEER 
// 
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at    
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt

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 

// Verify that all inputs are character strings
for k=1:size(varargin)
  if type(varargin(k))<>10 then
    error(msprintf(gettext("%s: Wrong type for input argument #%d: Matrix of strings expected.\n"),"loadmatfile"));
  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(msprintf(gettext("%s: This feature has not been implemented: %s."),"loadmatfile","-regexp"));
      while k<=lstsize(varargin) & and(varargin(k)<>["-mat","-ascii"])
      	k=k+1
      end
    else 
      if isempty(fil) then // Filename
      	fil=pathconvert(varargin(k),%f,%t);
      	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 // Uses MATIO interface
  
  Name='';
  Names=[];
  Matrices=list()
  
  //-- Try to open the file
  fd = matfile_open(pathconvert(fil, %F, %T), "r");
  if fd==-1 then
    error(msprintf(gettext("%s: Cannot open file %s.\n"),"loadmatfile",fil));
  end
  
  //-- Read first variable
  [Name, Matrix, Class] = matfile_varreadnext(fd);
  
  //-- Loop on the stored variables
  while Name<>""
    
    // Old version compatibility | Name has been given
    if isempty(varnames) | or(Name==varnames) then
      Names=[Names,Name];Matrices($+1)=Matrix
    end
    
    [Name, Matrix, Class] = matfile_varreadnext(fd);
    
  end
  
  //-- Close the file
  matfile_close(fd)
  
  //-- Error while reading?
  if isempty(Names) then
    error(msprintf(gettext("%s: No variable read in file ''%s''. Check if your file is not corrupted.\n"),"loadmatfile",fil));
  end
  
  //-- Return variables in the calling context
  execstr('['+strcat(Names,',')+']=resume(Matrices(:))')
   
// --- 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