source: aedes_readjcamp.m @ 199

Last change on this file since 199 was 194, checked in by tjniskan, 7 years ago
  • aedes_readjcamp now correctly reads matrices from JCAMP files into cell arrays.
  • aedes_readbruker now tries to read information from acqp and method files when opening a 2dseq file.
  • aedes_readbruker now reads also visu_pars file and uses that to read the 2dseq file if reco file is not found.

M aedes_readbruker.m
M aedes_readjcamp.m
M aedes_revision.m

File size: 4.3 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        param = regexp(str,'^(.*)=','tokens','once');
74        param = strrep(param{1},'$','');
75        param = l_CheckParameter(param);
76       
77        if any(str==sprintf('\n'))
78                % Get size
79                sz = regexp(str,'=\s*\((.*?)\)\s*\n','tokens','once');
80                sz = str2num(['[',sz{1},']']);
81               
82                % Parse value
83                value = regexp(str,'\n(.*)$','tokens','once');
84                value = value{1};
85                value = l_CheckValue(value,sz);
86        else
87                value = regexp(str,'=\s*(.*)','tokens','once');
88                value = value{1};
89                value = l_CheckValue(value);
90        end
91       
92        % Add to structure
93        jdx.(param) = value;
94       
95end
96
97
98
99
100
101% ==========================
102% - Subfunctions -
103% ==========================
104
105% - Remove linebreaks
106function out = l_RemoveLineBreaks(str)
107
108out = strrep(str,sprintf('\n'),'');
109
110
111% - Check parameter value --------------------------
112function out = l_CheckValue(val,sz)
113
114if nargin == 1
115        sz = 0;
116end
117
118% Remove insignificant whitespace
119val = strtrim(val);
120
121if isempty(val)
122        out = val;
123        return
124end
125
126% Handle strings and string lists
127if val(1) == '<' && val(end) == '>'
128        val(val=='<')='''';
129        val(val=='>')='''';
130        out = eval(['{',val,'}']);
131        if length(out) == 1
132                out = out{1};
133        end
134        return
135end
136
137% Handle cell matrices
138if val(1) == '(' && val(end) == ')'
139        nRows = length(find(val==')'));
140       
141        val(1) = '';
142        val(end) = '';
143        val(val=='(')='';
144        val(val==')')=',';
145        val(val=='<')='';
146        val(val=='>')='';
147       
148        % Split using the commas
149        val_split = regexp(val,',\s*','split');
150        val_out = cell(size(val_split));
151       
152        % Try to convert to numbers
153        for ii = 1:length(val_split)
154                num = str2double(val_split{ii});
155                if isnan(num)
156                        val_out{ii} = val_split{ii};
157                else
158                        val_out{ii} = num;
159                end
160        end
161       
162        out = reshape(val_out,[],nRows).';
163        return
164end
165
166% Check if the string contains only numbers before tryin to convert to a
167% number. str2num uses eval command and if the string matches to a
168% function name strange things can happen...
169tmp2 = regexp(val,'[^\d\.\seE-+]');
170if ~isempty(tmp2)
171        out = val;
172        return
173end
174
175% Convert value to numeric if possible
176tmp = str2num(val);
177if ~isempty(tmp) && isreal(tmp)
178        if length(sz)>1
179                tmp = reshape(tmp,sz(2),sz(1),[]);
180                tmp = permute(tmp,[2 1 3]);
181        end
182        out = tmp;
183        return
184end
185
186out = val;
187
188% - Check parameter strings -------------------------
189function out = l_CheckParameter(param)
190
191alphabets = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
192numbers = '1234567890';
193
194% Remove insignificant whitespace
195param = strtrim(param);
196
197if isempty(param)
198        out = [];
199        return
200end
201
202% Check parameter starts with a valid structure field character
203if ~any(param(1)==alphabets)
204        param = ['PAR_',param];
205end
206
207% Check that the parameter string does not contain any illegal characters
208% (for Matlab structure fields)
209ind = ~ismember(param,[alphabets,numbers,'_']);
210if any(ind)
211        param(ind) = '_';
212end
213
214out = param;
Note: See TracBrowser for help on using the repository browser.

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