NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/util/loadsafe.m

Revision 495 (checked in by cbc, 12 years ago)

Initial import of Stark code.

Line 
1 function [theResult, theLineNo] = loadsafe(theFilename)
2
3 % loadsafe -- Load an ascii file.
4 %  loadsafe('theFilename') loads the data from
5 %   'theFilename', an ascii file, alloting one
6 %   row of the result for each line of the file.
7 %   Warnings about questionable lines are posted.
8 %   No-data entries are marked by NaN.  Matlab
9 %   comments and blank-lines are ignored.  If
10 %   no output argument is given, the result is
11 %   assigned to the root-name of the file, as
12 %   with regular Matlab "load".
13 %  [theResult, theLineNo] = ... also returns the
14 %   original line-numbers corresponding to the
15 %   rows of theResult.
16  
17 % Copyright (C) 1998 Dr. Charles R. Denham, ZYDECO.
18 %  All Rights Reserved.
19 %   Disclosure without explicit written consent from the
20 %    copyright owner does not constitute publication.
21  
22 % Version of 19-Oct-1998 22:36:04.
23 % Updated    20-Oct-1998 13:38:57.
24
25 if nargout > 0, theResult = []; end
26
27 if nargin < 1
28         help(mfilename)
29         theFilename = '*';
30 end
31
32 if any(theFilename == '*')
33         [theFile, thePath] = uigetfile(theFilename, 'Select a File:');
34         if ~any(theFile), return, end
35         if thePath(length(thePath) ~= filesep)
36                 thePath = [thePath filesep];
37         end
38         theFilename = [thePath theFile];
39 end
40
41 fp = fopen(theFilename, 'r');
42
43 count = 0;
44 bad = 0;
45 len = 0;
46 max_len = 0;
47 while 1
48         s = fgetl(fp);
49         if isequal(s, -1), break, end
50         f = find(s == '%');
51         if any(f), s(f(1):length(s)) = ''; end
52         while(length(s) > 0 & s(1) == ' ')
53                 s(1) = '';
54         end
55         if length(s) > 0 & s(1) ~= '%'
56                 count = count+1;
57                 t = ['[' s ']'];
58                 bad_line = 0;
59                 data = [];
60                 eval('data = eval(t);', 'bad_line = 1;');
61                 if ~bad_line
62                         max_len = max(max_len, length(data));
63                 else
64                         bad = bad + 1;
65                         disp([' ## ??? line ' int2str(count) ': ' s])
66                 end
67         end
68 end
69
70 if bad > 0
71         disp(' ')
72         disp([' ## Number of questionable lines: ' int2str(bad)])
73         disp(' ')
74 end
75
76 frewind(fp)
77
78 result = nan * zeros(count, max_len);
79
80 lineno = zeros(count, 1);
81 line = 0;
82
83 count = 0;
84
85 while (1)
86         s = fgetl(fp);
87         if isequal(s, -1), break, end
88         line = line + 1;
89         while(length(s) > 0 & s(1) == ' ')
90                 s(1) = '';
91         end
92         if length(s) > 0 & s(1) ~= '%'
93                 count = count+1;
94                 lineno(count) = line;
95                 t = ['[' s ']'];
96                 bad_line = 0;
97                 data = [];
98                 eval('data = eval(t);', 'bad_line = 1;');
99                 if ~bad_line & length(data) > 0
100                         result(count, 1:length(data)) = data;
101                 end
102         end
103 end
104
105 fclose(fp);
106
107 if nargout > 0
108         theResult = result;
109         theLineNo = lineno;
110 else
111         theName = theFilename;
112         f = find(theName == filesep);
113         if any(f), theName(1:f(length(f))) = ''; end
114         f = find(theName == '.');
115         if any(f), theName(f(1):length(theName)) = ''; end
116         assignin('caller', theName, result)
117 end
Note: See TracBrowser for help on using the browser.