forked from nadegecorbin/Reco_MESE_RAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createNifti.m
executable file
·92 lines (79 loc) · 2.35 KB
/
createNifti.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
%**************************************************************************
%
% Writes out data in nifti format. If the data is complex then it is
% written out as two files with the suffix _Magnitude & _Phase.
%
% MFC 14.12.2012
%
% MFC 01.05.2013 Oddly I had dat(:,:,:) = data so changed now to
% N.dat(:,:,:) = data
%
% MFC 21.05.2013 Extra input parameter of spmMatrix so as to have
% orientational information.
%
% MFC 10.10.2013 The phase data was being written before the spmMatrix
% was being set!!
%
%**************************************************************************
function createNifti(data, fn, spmMatrix)
% If a filename hasn't been passed in, where should I put it?
if nargin < 2
[fn pn] = uiputfile('*.nii', 'Save As...');
[~, fn] = fileparts(fn);
else
[pn fn] = fileparts(fn);
if isempty(pn)
pn = pwd;
end
end
% Is the data real or complex?
if isreal(data)
dat = file_array;
dat.fname = [pn filesep fn '.nii'];
dat.dim = size(data);
dat.offset = ceil(348/8)*8;
if isfloat(data)
dat.dtype = 'FLOAT32';
N.descrip = 'FLOAT data';
else
dat.dtype = 'UINT8';
N.descrip = 'Non-float data';
end
% Create the NIFTI structure
N = nifti;
N.dat = dat;
if exist('spmMatrix', 'var')
N.mat = spmMatrix;
N.mat0 = spmMatrix;
end
create(N); % Writes hdr
N.dat(:,:,:) = data; % Writes data
else
% General file_array fields:
dat = file_array;
dat.dim = size(data);
dat.dtype = 'FLOAT32';
dat.offset = ceil(348/8)*8;
% Create the NIFTI structure for the magnitude:
dat.fname = [pn filesep fn '_Magnitude.nii'];
N = nifti;
N.dat = dat;
N.descrip = 'Magnitude (from complex)';
if exist('spmMatrix', 'var')
N.mat = spmMatrix;
N.mat0=spmMatrix;
end
create(N); % Writes hdr info
N.dat(:,:,:) = abs(data); % writes data to disk
% Create the NIFTI structure for the phase:
N = nifti;
dat.fname = [pn filesep fn '_Phase.nii'];
N.dat = dat;
N.descrip = 'Phase (from complex)';
if exist('spmMatrix', 'var')
N.mat = spmMatrix;
N.mat0 = spmMatrix;
end
create(N);
N.dat(:,:,:) = angle(data);
end