source: aedes_readjcamp.m @ 206

Last change on this file since 206 was 206, checked in by tjniskan, 6 years ago

-Fixed a rare regexp related bug in aedes_readjcamp

M aedes_readjcamp.m
M aedes_revision.m

File size: 4.5 KB
Line 
1function jdx = aedes_readjcamp(filename)
2% AEDES_READJCAMP - Read JCAMP DX format files (Bruker parameter files)
3%   
4%
5% Synopsis:
6%       jdx=aedes_readjcamp(filename)
7%
8% Description:
9%       The function reads the JCAMP DX files and returns a
10%       structure with parameters as structure fields. The input
11%       argument is a string containing the full path to the file.
12%
13% Examples:
14%       jdx=aedes_readjcamp('C:\path\to\jcamp_dx_file')
15%
16% See also:
17%       AEDES_READBRUKER, AEDES_DATA_READ, AEDES
18
19jdx = [];
20
21% Prompt for a file if not given as an input argument
22if nargin == 0
23        [fn,fp] = uigetfile({'*.*','All Files (*.*)'},'Open a JCAMP DX file');
24        if isequal(fn,0)
25                return
26        end
27        filename = [fp,fn];
28elseif nargin > 1
29        error('Too many input arguments.');
30end
31
32% Open the file for reading
33fid = fopen(filename,'r');
34if fid < 0
35        error('Could not open file "%s" for reading.',filename);
36end
37
38% Check that the file is a JCAMP DX file
39str = fread(fid,20,'char=>char');
40if isempty(regexp(str.','^\s*##TITLE'))
41        fclose(fid);
42        error('File "%s" is not a valid JCAMP DX file.',filename)
43end
44fseek(fid,0,-1); % Rewind file
45
46C = fread(fid,inf,'char');
47fclose(fid);
48
49% Remove carriage returns
50C(C==13)=[];
51
52% Convert to string
53C = char(C.');
54
55% Remove comment lines
56C = regexprep(C,'\$\$([^\n]*)\n','');
57
58% Remove unnecessary line breaks
59f = @l_RemoveLineBreaks;
60C=regexprep(C,'^(\s*[^#].*?)(?=\n\s*#)','${f($1)}','lineanchors');
61C=regexprep(C,'(\([^\)]+?)\n(.*?\))','${f([$1,$2])}','lineanchors');
62CC = regexp(C,'\s*##','split');
63CC(1)=[];
64
65% Parse the file line-by-line
66for ii=1:length(CC)
67       
68        str = CC{ii};
69        if strncmp(str,'END=',4)
70                continue
71        end
72       
73        % The commented regexp sometimes fails with long strings...
74        %param = regexp(str,'^(.*)=','tokens','once');
75        ind = find(str==61); % Find '=' chars...
76        if isempty(ind)
77                param='';
78        else
79                param=str(1:ind(1)-1);
80        end
81        %param = strrep(param{1},'$','');
82        param = strrep(param,'$','');
83        param = l_CheckParameter(param);
84       
85        if any(str==sprintf('\n'))
86                % Get size
87                sz = regexp(str,'=\s*\((.*?)\)\s*\n','tokens','once');
88                sz = str2num(['[',sz{1},']']);
89               
90                % Parse value
91                value = regexp(str,'\n(.*)$','tokens','once');
92                value = value{1};
93                value = l_CheckValue(value,sz);
94        else
95                value = regexp(str,'=\s*(.*)','tokens','once');
96                value = value{1};
97                value = l_CheckValue(value);
98        end
99       
100        % Add to structure
101        jdx.(param) = value;
102       
103end
104
105
106
107
108
109% ==========================
110% - Subfunctions -
111% ==========================
112
113% - Remove linebreaks
114function out = l_RemoveLineBreaks(str)
115
116out = strrep(str,sprintf('\n'),'');
117
118
119% - Check parameter value --------------------------
120function out = l_CheckValue(val,sz)
121
122if nargin == 1
123        sz = 0;
124end
125
126% Remove insignificant whitespace
127val = strtrim(val);
128
129if isempty(val)
130        out = val;
131        return
132end
133
134% Handle strings and string lists
135if val(1) == '<' && val(end) == '>'
136        val(val=='<')='''';
137        val(val=='>')='''';
138        out = eval(['{',val,'}']);
139        if length(out) == 1
140                out = out{1};
141        end
142        return
143end
144
145% Handle cell matrices
146if val(1) == '(' && val(end) == ')'
147        nRows = length(find(val==')'));
148       
149        val(1) = '';
150        val(end) = '';
151        val(val=='(')='';
152        val(val==')')=',';
153        val(val=='<')='';
154        val(val=='>')='';
155       
156        % Split using the commas
157        val_split = regexp(val,',\s+','split');
158        val_out = cell(size(val_split));
159       
160        % Try to convert to numbers
161        for ii = 1:length(val_split)
162                num = str2double(val_split{ii});
163                if isnan(num)
164                        val_out{ii} = val_split{ii};
165                else
166                        val_out{ii} = num;
167                end
168        end
169       
170       
171        out = reshape(val_out,[],nRows).';
172        return
173end
174
175% Check if the string contains only numbers before tryin to convert to a
176% number. str2num uses eval command and if the string matches to a
177% function name strange things can happen...
178tmp2 = regexp(val,'[^\d\.\seE-+]');
179if ~isempty(tmp2)
180        out = val;
181        return
182end
183
184% Convert value to numeric if possible
185tmp = str2num(val);
186if ~isempty(tmp) && isreal(tmp)
187        if length(sz)>1
188                tmp = reshape(tmp,sz(2),sz(1),[]);
189                tmp = permute(tmp,[2 1 3]);
190        end
191        out = tmp;
192        return
193end
194
195out = val;
196
197% - Check parameter strings -------------------------
198function out = l_CheckParameter(param)
199
200alphabets = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
201numbers = '1234567890';
202
203% Remove insignificant whitespace
204param = strtrim(param);
205
206if isempty(param)
207        out = 'EMPTY_PARAM';
208        return
209end
210
211% Check parameter starts with a valid structure field character
212if ~any(param(1)==alphabets)
213        param = ['PAR_',param];
214end
215
216% Check that the parameter string does not contain any illegal characters
217% (for Matlab structure fields)
218ind = ~ismember(param,[alphabets,numbers,'_']);
219if any(ind)
220        param(ind) = '_';
221end
222
223out = param;
Note: See TracBrowser for help on using the repository browser.

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008