source: aedes_readjcamp.m

Last change on this file was 207, checked in by tjniskan, 4 years ago
  • Fixed a bug with nested tables in aedes_readjcamp

M aedes_readjcamp.m
M aedes_revision.m

File size: 4.7 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        % Nested tables are not supported. This is a workaround for nested tables
150        % and everything is read in a single lined table...
151        if nRows ~= sz && sz>0
152                nRows=sz;
153        end
154       
155        val(1) = '';
156        val(end) = '';
157        val(val=='(')='';
158        val(val==')')=',';
159        val(val=='<')='';
160        val(val=='>')='';
161       
162        % Split using the commas
163        val_split = regexp(val,',\s+','split');
164        val_out = cell(size(val_split));
165       
166        % Try to convert to numbers
167        for ii = 1:length(val_split)
168                num = str2double(val_split{ii});
169                if isnan(num)
170                        val_out{ii} = val_split{ii};
171                else
172                        val_out{ii} = num;
173                end
174        end
175       
176       
177        out = reshape(val_out,[],nRows).';
178        return
179end
180
181% Check if the string contains only numbers before tryin to convert to a
182% number. str2num uses eval command and if the string matches to a
183% function name strange things can happen...
184tmp2 = regexp(val,'[^\d\.\seE-+]');
185if ~isempty(tmp2)
186        out = val;
187        return
188end
189
190% Convert value to numeric if possible
191tmp = str2num(val);
192if ~isempty(tmp) && isreal(tmp)
193        if length(sz)>1
194                tmp = reshape(tmp,sz(2),sz(1),[]);
195                tmp = permute(tmp,[2 1 3]);
196        end
197        out = tmp;
198        return
199end
200
201out = val;
202
203% - Check parameter strings -------------------------
204function out = l_CheckParameter(param)
205
206alphabets = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
207numbers = '1234567890';
208
209% Remove insignificant whitespace
210param = strtrim(param);
211
212if isempty(param)
213        out = 'EMPTY_PARAM';
214        return
215end
216
217% Check parameter starts with a valid structure field character
218if ~any(param(1)==alphabets)
219        param = ['PAR_',param];
220end
221
222% Check that the parameter string does not contain any illegal characters
223% (for Matlab structure fields)
224ind = ~ismember(param,[alphabets,numbers,'_']);
225if any(ind)
226        param(ind) = '_';
227end
228
229out = param;
Note: See TracBrowser for help on using the repository browser.

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