source: aedes_readjcamp.m @ 172

Last change on this file since 172 was 172, checked in by tjniskan, 8 years ago
  • Added a function for reading JCAMP-DX files (for upcoming Bruker data support).

A aedes_readjcamp.m
M aedes_revision.m

File size: 3.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
46% Read the whole file into a cell
47% try
48%   C = textscan(fid,'%s','delimiter','\n','BufSize',1024*1024);
49%   jdx_str=C{1};
50%       fclose(fid);
51% catch
52%       fclose(fid);
53%   error('Error while reading procpar file. %s',lasterr);
54% end
55C = fread(fid,inf,'char');
56fclose(fid);
57
58% Remove carriage returns
59C(C==13)=[];
60
61% Convert to string
62C = char(C.');
63
64% Remove comment lines
65C = regexprep(C,'\$\$([^\n]*)\n','');
66
67% Remove unnecessary line breaks
68f = @l_RemoveLineBreaks;
69C=regexprep(C,'^(\s*[^#].*?)(?=\n\s*#)','${f($1)}','lineanchors');
70C=regexprep(C,'(\([^\)]+?)\n(.*?\))','${f([$1,$2])}','lineanchors');
71CC = regexp(C,'\s*##','split');
72CC(1)=[];
73
74% Parse the file line-by-line
75for ii=1:length(CC)
76       
77        str = CC{ii};
78        if strncmp(str,'END=',4)
79                continue
80        end
81       
82        param = regexp(str,'^(.*)=','tokens','once');
83        param = strrep(param{1},'$','');
84        param = l_CheckParameter(param);
85       
86        if any(str==sprintf('\n'))
87                % Get size
88                sz = regexp(str,'=\s*\((.*?)\)\s*\n','tokens','once');
89                sz = str2num(['[',sz{1},']']);
90               
91                % Parse value
92                value = regexp(str,'\n(.*)$','tokens','once');
93                value = value{1};
94                value = l_CheckValue(value,sz);
95        else
96                value = regexp(str,'=\s*(.*)','tokens','once');
97                value = value{1};
98                value = l_CheckValue(value);
99        end
100       
101        % Add to structure
102        jdx.(param) = value;
103       
104end
105
106
107
108
109
110% ==========================
111% - Subfunctions -
112% ==========================
113
114% - Remove linebreaks
115function out = l_RemoveLineBreaks(str)
116
117out = strrep(str,sprintf('\n'),'');
118
119
120% - Check parameter value --------------------------
121function out = l_CheckValue(val,sz)
122
123if nargin == 1
124        sz = 0;
125end
126
127% Remove insignificant whitespace
128val = strtrim(val);
129
130if isempty(val)
131        out = val;
132        return
133end
134
135% Remove string identifiers
136if val(1) == '<' && val(end) == '>'
137        val = val(2:end-1);
138        out = val;
139        return
140end
141
142% Convert value to numeric if possible
143tmp = str2num(val);
144if ~isempty(tmp) && isreal(tmp)
145        if length(sz)>1
146                tmp = reshape(tmp,sz(2),sz(1),[]);
147                tmp = permute(tmp,[2 1 3]);
148        end
149        out = tmp;
150        return
151end
152
153out = val;
154
155% - Check parameter strings -------------------------
156function out = l_CheckParameter(param)
157
158alphabets = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
159numbers = '1234567890';
160
161% Remove insignificant whitespace
162param = strtrim(param);
163
164if isempty(param)
165        out = [];
166        return
167end
168
169% Check parameter starts with a valid structure field character
170if ~any(param(1)==alphabets)
171        param = ['PAR_',param];
172end
173
174% Check that the parameter string does not contain any illegal characters
175% (for Matlab structure fields)
176ind = ~ismember(param,[alphabets,numbers,'_']);
177if any(ind)
178        param(ind) = '_';
179end
180
181out = param;
Note: See TracBrowser for help on using the repository browser.

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