Browse code

searchlight ready. missing nifti-image-write support. added timing to FBS

git-svn-id: https://svn.discofish.de/MATLAB/spmtoolbox/SVMCrossVal@167 83ab2cfd-5345-466c-8aeb-2b2739fb922d

Christoph Budziszewski authored on30/03/2009 17:54:25
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,83 @@
1
+%MAT_INTO_HDR  The old versions of SPM (any version before SPM5) store
2
+%	an affine matrix of the SPM Reoriented image into a matlab file 
3
+%	(.mat extension). The file name of this SPM matlab file is the
4
+%	same as the SPM Reoriented image file (.img/.hdr extension).
5
+%
6
+%	This program will convert the ANALYZE 7.5 SPM Reoriented image
7
+%	file into NIfTI format, and integrate the affine matrix in the
8
+%	SPM matlab file into its header file (.hdr extension).
9
+%
10
+%	WARNING: Before you run this program, please save the header
11
+%	file (.hdr extension) into another file name or into another
12
+%	folder location, because all header files (.hdr extension)
13
+%	will be overwritten after they are converted into NIfTI
14
+%	format.
15
+%
16
+%  Usage: mat_into_hdr(filename);
17
+%
18
+%  filename:	file name(s) with .hdr or .mat file extension, like:
19
+%		'*.hdr', or '*.mat', or a single .hdr or .mat file.
20
+%	e.g.	mat_into_hdr('T1.hdr')
21
+%		mat_into_hdr('*.mat')
22
+%
23
+
24
+%  - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
25
+%
26
+%-------------------------------------------------------------------------
27
+function mat_into_hdr(files)
28
+
29
+   pn = fileparts(files);
30
+   file_lst = dir(files);
31
+   file_lst = {file_lst.name};
32
+   file1 = file_lst{1};
33
+   [p n e]= fileparts(file1);
34
+
35
+   for i=1:length(file_lst)
36
+      [p n e]= fileparts(file_lst{i});
37
+      disp(['working on file ', num2str(i) ,' of ', num2str(length(file_lst)), ': ', n,e]);
38
+      process=1;
39
+
40
+      if isequal(e,'.hdr')
41
+         mat=fullfile(pn, [n,'.mat']);
42
+         hdr=fullfile(pn, file_lst{i});
43
+
44
+         if ~exist(mat,'file')
45
+            warning(['Cannot find file "',mat  , '". File "', n, e, '" will not be processed.']);
46
+            process=0;
47
+         end
48
+      elseif isequal(e,'.mat')
49
+         hdr=fullfile(pn, [n,'.hdr']);
50
+         mat=fullfile(pn, file_lst{i});
51
+
52
+         if ~exist(hdr,'file')
53
+            warning(['Can not find file "',hdr  , '". File "', n, e, '" will not be processed.']);
54
+            process=0;
55
+         end
56
+      else
57
+         warning(['Input file must have .mat or .hdr extension. File "', n, e, '" will not be processed.']);
58
+         process=0;
59
+      end
60
+
61
+      if process
62
+         load(mat);
63
+         R=M(1:3,1:3);
64
+         T=M(1:3,4);
65
+         T=R*ones(3,1)+T;
66
+         M(1:3,4)=T;
67
+
68
+         [h filetype fileprefix machine]=load_nii_hdr(hdr);
69
+         h.hist.qform_code=0;
70
+         h.hist.sform_code=1;
71
+         h.hist.srow_x=M(1,:);
72
+         h.hist.srow_y=M(2,:);
73
+         h.hist.srow_z=M(3,:);
74
+         h.hist.magic='ni1';
75
+
76
+         fid = fopen(hdr,'w',machine);
77
+         save_nii_hdr(h,fid);
78
+         fclose(fid);
79
+      end
80
+   end
81
+
82
+   return;				% mat_into_hdr
83
+