source: an2_readfdf.m @ 44

Last change on this file since 44 was 39, checked in by tjniskan, 11 years ago
  • Added "Recent Files" quick access menu in File-menu
  • Fixed a bug in plugins/copy_data_to_workspace.m that made writing a

new name for a variable difficult

  • Fixed a bug in the file prefix editbox in an2_export_gui.m
  • Changed references to Analyze 2.0 in all license notices to Aedes.

The name change should now be complete...

M an2_export_gui.m
M an2_cellsprintf.m
M an2_calc_wait.m
M an2_check_file_exist.m
M an2_iseven.m
M an2_cellwrite.m
M an2_wbar.m
M an2_rot3d.m
M an2_readfdf.m
M an2_revision.m
M an2_viewprocpar.m
M an2_checkcthdr.m
M an2_readprocpar.m
M an2_fitmaps.m
M an2_read_nifti.m
M an2_data_read.m
M an2_resviewer.m
M an2_maptool.m
M aedes.m
M an2_res2table.m
M an2_copy_roi.m
M plugins/save_roi_as_mask.m
M plugins/write_difference_images.m
M plugins/plot_profile.m
M plugins/calculate_t2_map.m
M plugins/calculate_t1r_map.m
M plugins/view_kspace.m
M plugins/copy_data_to_workspace.m
M plugins/take_snapshot.m
M an2_inputdlg.m
M an2_roi_copy_gui.m
M an2_readctdata.m
M an2_readfid.m
M an2_readfidprefs.m
M an2_readtab.m
M an2_check_updates.m
M an2_killfigs.m
M an2_roi_stats.m
M an2_saveres.m
M an2_rotateflip.m
M an2_juigetfiles.m
M an2_gui_defaults.m
M an2_editstack.m
M an2_update.m
M an2_write_nifti.m

File size: 3.9 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, (or 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-be');
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% Read Data
62[data,msg]=l_ReadData(FileHeader,file_fid);
63
64% Close file
65fclose(file_fid);
66
67% Construct DATA structure
68DATA.DataFormat = 'FDF';
69DATA.HDR.FileHeader = FileHeader;
70DATA.HDR.fname = [fn,fe];
71DATA.HDR.fpath = [fp,filesep];
72DATA.FTDATA = data;
73
74
75
76%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77% Read Data Header
78%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79function [FileHeader,msg]=l_ReadHdr(file_fid)
80
81FileHeader=[];
82msg = '';
83
84
85%% Check if file is a valid FDF-file
86magic_str=char(fread(file_fid,25,'char'))';
87if length(magic_str)<25 || ~strcmpi(magic_str(1:end-1),'#!/usr/local/fdf/startup')
88  msg='File is not a valid FDF-file!';
89  return
90end
91
92% Seek the file back to beginning
93
94try
95%% Read ASCII header to a cell array
96done=false;
97count=0;
98header={};
99while ~done
100  count=count+1;
101 
102  % Check first char on the line
103  isnull=double(fread(file_fid,1,'char'))==0;
104  if isnull
105    done=true;
106    continue;
107  else
108    % Move file indicator back
109    fseek(file_fid,-1,0);
110  end
111 
112  header{count}=fgetl(file_fid);
113end
114
115
116
117%% Parse the header cell array
118for ii=1:length(header)
119  if length(header{ii})<=2 || all(header{ii}==32) || ...
120        all(header{ii}==0)
121    continue;
122  end
123 
124  % Remove * and [] chars
125  header{ii}=strrep(header{ii},'[','');
126  header{ii}=strrep(header{ii},']','');
127  header{ii}=strrep(header{ii},'*','');
128 
129  % Replace " chars with '
130  header{ii}=strrep(header{ii},'"','''');
131 
132  %% Find the first space char
133  ind=find(header{ii}==32);
134  if length(ind)>1 && diff([ind(1:2)])==1
135    eval(['FileHeader.',header{ii}(ind(2)+1:end)])
136  else
137    eval(['FileHeader.',header{ii}(ind(1)+1:end)])
138  end
139end
140
141%% Check that numeric fields are numeric
142fldnames=fieldnames(FileHeader);
143for ii=1:length(fldnames)
144  if iscell(FileHeader.(fldnames{ii}))
145    if all(cellfun(@isnumeric,FileHeader.(fldnames{ii})))
146      tmp=FileHeader.(fldnames{ii});
147      FileHeader.(fldnames{ii}) = [tmp{:}];
148    end
149  end
150end
151
152catch
153  FileHeader=[];
154  msg='Error occurred while reading FDF file header!';
155end
156
157
158%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159% Read Data
160%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161function [data,msg]=l_ReadData(FileHeader,file_fid)
162
163data=[];
164msg='';
165
166%% Read data parameters
167data_sz = FileHeader.matrix;
168datatype = FileHeader.storage;
169bits = FileHeader.bits;
170dataformat=FileHeader.type;
171
172if strcmpi(datatype,'integer')
173  data_str = ['*int',num2str(bits)];
174elseif strcmpi(datatype,'float')
175  data_str = ['*float',num2str(bits)];
176end
177
178%% Read data
179[data,count]=fread(file_fid,inf,data_str);
180data=reshape(data,data_sz);
181%for ii=1:size(data,3)
182%  data(:,:,ii)=rot90(data(:,:,ii));
183%end
184
Note: See TracBrowser for help on using the repository browser.

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