source: aedes_readjcamp.m @ 187

Last change on this file since 187 was 173, checked in by tjniskan, 8 years ago
  • Added a function (fdf2nifti.m) for writing Varian FDF-files into a NIfTI file
  • Fixed a small bug in aedes_headerbrowser.m

A misclib/fdf2nifti.m
M aedes_readjcamp.m
M aedes_headerbrowser.m
M aedes_revision.m

File size: 3.4 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% Remove string identifiers
127if val(1) == '<' && val(end) == '>'
128        val = val(2:end-1);
129        out = val;
130        return
131end
132
133% Convert value to numeric if possible
134tmp = str2num(val);
135if ~isempty(tmp) && isreal(tmp)
136        if length(sz)>1
137                tmp = reshape(tmp,sz(2),sz(1),[]);
138                tmp = permute(tmp,[2 1 3]);
139        end
140        out = tmp;
141        return
142end
143
144out = val;
145
146% - Check parameter strings -------------------------
147function out = l_CheckParameter(param)
148
149alphabets = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
150numbers = '1234567890';
151
152% Remove insignificant whitespace
153param = strtrim(param);
154
155if isempty(param)
156        out = [];
157        return
158end
159
160% Check parameter starts with a valid structure field character
161if ~any(param(1)==alphabets)
162        param = ['PAR_',param];
163end
164
165% Check that the parameter string does not contain any illegal characters
166% (for Matlab structure fields)
167ind = ~ismember(param,[alphabets,numbers,'_']);
168if any(ind)
169        param(ind) = '_';
170end
171
172out = param;
Note: See TracBrowser for help on using the repository browser.

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