source: an2_readfdf.m @ 62

Last change on this file since 62 was 62, checked in by tjniskan, 11 years ago
  • Added functions for basic fMRI analysis and resting-state
  • Added trend in the "show voxel timeseries" figure. In EPI datas the

the reference image is excluded from the time series and the y-axis
units are changed to "BOLD (%)".

  • Fixed an endiannes bug in an2_readfdf.m
  • Added plugins basic fmri analysis and resting-state analysis

M misclib/fmri_spm_volumes.m
A misclib/fmri_analysis.m
M an2_readfdf.m
M an2_revision.m
M an2_fitmaps.m
M an2_data_read.m
M aedes.m
A plugins/basic_fmri_analysis.m
A plugins/resting_state_fc.m

File size: 4.3 KB
Line 
1function DATA = an2_readfdf(filename,varargin)
2% AN2_READFDF - Read Varian Flexible Data Format (FDF) Files
3%   
4%
5% Synopsis:
6%
7% Description:
8%
9% Examples:
10%
11% See also:
12%
13
14% This function is a part of Aedes - A graphical tool for analyzing
15% medical images
16%
17% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
18%
19% Department of Physics, Department of Neurobiology
20% University of Kuopio, FINLAND
21%
22% This program may be used under the terms of the GNU General Public
23% License version 2.0 as published by the Free Software Foundation
24% and appearing in the file LICENSE.TXT included in the packaging of
25% this program.
26%
27% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29
30
31if nargin==0 || isempty(filename)
32  %% Ask for a file name
33  [f_name, f_path, f_index] = uigetfile( ...
34    {'*.fdf;*.FDF','Varian FDF-files (*.fdf)'; ...
35     '*.*', 'All Files (*.*)'}, ...
36    'Select Varian FDF-file');
37  if ( all(f_name==0) | all(f_path==0) ) % Cancel is pressed
38    DATA=[];
39    return
40  else
41    filename = [f_path,f_name];
42  end
43end
44
45% Try to open file for reading
46file_fid = fopen(filename,'r','ieee-le');
47if file_fid<0
48  DATA=[];
49  error('Could not open file "%s" for reading!',filename);
50end
51
52[fp,fn,fe]=fileparts(filename);
53
54% Read Header
55[FileHeader,msg]=l_ReadHdr(file_fid);
56if isempty(FileHeader)
57  DATA=[];
58  error(msg);
59end
60
61% Check endianness
62if isfield(FileHeader,'bigendian') && FileHeader.bigendian==1
63  pos = ftell(file_fid);
64  fclose(file_fid);
65  file_fid = fopen(filename,'r','ieee-be');
66  if file_fid<0
67    DATA=[];
68    error('Could not open file "%s" for reading!',filename);
69  end
70 
71  % Seek to data start
72  fseek(file_fid,pos,-1);
73end
74
75% Read Data
76[data,msg]=l_ReadData(FileHeader,file_fid);
77
78% Close file
79fclose(file_fid);
80
81% Construct DATA structure
82DATA.DataFormat = 'FDF';
83DATA.HDR.FileHeader = FileHeader;
84DATA.HDR.fname = [fn,fe];
85DATA.HDR.fpath = [fp,filesep];
86DATA.FTDATA = data;
87
88
89
90%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91% Read Data Header
92%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93function [FileHeader,msg]=l_ReadHdr(file_fid)
94
95FileHeader=[];
96msg = '';
97
98
99%% Check if file is a valid FDF-file
100magic_str=char(fread(file_fid,25,'char'))';
101if length(magic_str)<25 || ~strcmpi(magic_str(1:end-1),'#!/usr/local/fdf/startup')
102  msg='File is not a valid FDF-file!';
103  return
104end
105
106% Seek the file back to beginning
107
108try
109%% Read ASCII header to a cell array
110done=false;
111count=0;
112header={};
113while ~done
114  count=count+1;
115 
116  % Check first char on the line
117  isnull=double(fread(file_fid,1,'char'))==0;
118  if isnull
119    done=true;
120    continue;
121  else
122    % Move file indicator back
123    fseek(file_fid,-1,0);
124  end
125 
126  header{count}=fgetl(file_fid);
127end
128
129
130
131%% Parse the header cell array
132for ii=1:length(header)
133  if length(header{ii})<=2 || all(header{ii}==32) || ...
134        all(header{ii}==0)
135    continue;
136  end
137 
138  % Remove * and [] chars
139  header{ii}=strrep(header{ii},'[','');
140  header{ii}=strrep(header{ii},']','');
141  header{ii}=strrep(header{ii},'*','');
142 
143  % Replace " chars with '
144  header{ii}=strrep(header{ii},'"','''');
145 
146  %% Find the first space char
147  ind=find(header{ii}==32);
148  if length(ind)>1 && diff([ind(1:2)])==1
149    eval(['FileHeader.',header{ii}(ind(2)+1:end)])
150  else
151    eval(['FileHeader.',header{ii}(ind(1)+1:end)])
152  end
153end
154
155%% Check that numeric fields are numeric
156fldnames=fieldnames(FileHeader);
157for ii=1:length(fldnames)
158  if iscell(FileHeader.(fldnames{ii}))
159    if all(cellfun(@isnumeric,FileHeader.(fldnames{ii})))
160      tmp=FileHeader.(fldnames{ii});
161      FileHeader.(fldnames{ii}) = [tmp{:}];
162    end
163  end
164end
165
166catch
167  FileHeader=[];
168  msg='Error occurred while reading FDF file header!';
169end
170
171
172%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173% Read Data
174%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175function [data,msg]=l_ReadData(FileHeader,file_fid)
176
177data=[];
178msg='';
179
180%% Read data parameters
181data_sz = FileHeader.matrix;
182datatype = FileHeader.storage;
183bits = FileHeader.bits;
184dataformat=FileHeader.type;
185
186if strcmpi(datatype,'integer')
187  data_str = ['*int',num2str(bits)];
188elseif strcmpi(datatype,'float')
189  data_str = ['*float',num2str(bits)];
190end
191
192%% Read data
193[data,count]=fread(file_fid,inf,data_str);
194data=reshape(data,data_sz);
195
196% Permute to correct orientation
197data=permute(data,[2 1 3 4]);
198
199%for ii=1:size(data,3)
200%  data(:,:,ii)=rot90(data(:,:,ii));
201%end
202
Note: See TracBrowser for help on using the repository browser.

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