-
Notifications
You must be signed in to change notification settings - Fork 28
/
palm_hemimerge.m
95 lines (84 loc) · 2.67 KB
/
palm_hemimerge.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
93
94
95
function palm_hemimerge(varargin)
% Merge data for lh and rh (left and right hemispheres) into a
% single file. Various input files can be given, starting with
% 'lh' and/or 'rh'. The script finds the correct pair, removes
% repeated files, and merges them into files beginning with 'bh'.
%
% palm_hemimerge <files>
%
% Wildcards are accepted.
% _____________________________________
% Anderson M. Winkler
% FMRIB / University of Oxford
% Jun/2016
% http://brainder.org
% List of files (with wildcards)
j = 1;
for a = 1:nargin
F = dir(varargin{a});
for f = numel(F):-1:1
if F(f).name(1) == '.'
F(f) = [];
else
Flist{j} = F(f).name;
j = j + 1;
end
end
end
Flist = flipud(Flist');
% Prepare pairs to merge and output names
F = cell(0,3);
f = 1;
for a = numel(Flist):-1:1
if strcmpi(Flist{a}(1:2),'lh')
if ~ any(strcmpi(Flist{a},F(:,1)))
F{f,1} = Flist{a};
F{f,2} = strcat('rh',Flist{a}(3:end));
F{f,3} = strcat('bh',Flist{a}(3:end));
f = f + 1;
end
elseif strcmpi(Flist{a}(1:2),'rh')
if ~ any(strcmpi(Flist{a},F(:,2)))
F{f,1} = strcat('lh',Flist{a}(3:end));
F{f,2} = Flist{a};
F{f,3} = strcat('bh',Flist{a}(3:end));
f = f + 1;
end
else
warning('Files for merger must start with "lh" or "rh". Skipping: %s\n',Flist{a});
end
end
% For each valid pair
for f = 1:size(F,1)
fprintf('Working on: %s and %s\n',F{f,1},F{f,2});
% Load L and R
L = palm_miscread(F{f,1});
R = palm_miscread(F{f,2});
if ~ strcmpi(L.readwith,R.readwith)
error('lh and rh must be of the same type and format');
end
% Prepare data to save
B = L;
switch R.readwith
case {'load','csvread','fs_load_mgh'}
B.data = cat(1,L.data,R.data);
case 'dpxread'
B.data = cat(1,L.data,R.data);
B.extra.crd = cat(1,L.extra.crd,R.extra.crd);
B.extra.idx = (0:size(B.data,1)-1)';
case {'srfread','fs_read_surf'}
B.data.vtx = cat(1,L.data.vtx,R.data.vtx);
B.data.fac = cat(1,L.data.fac,R.data.fac+size(L.data.vtx,1));
case 'fs_read_curv'
B.data = cat(1,L.data,R.data);
B.extra.fnum = L.extra.fnum + R.extra.fnum;
otherwise
warning('Cannot deal with files read with %s. Skipping: ?%s\n',L.readwith,Flist{a}(2:end));
end
% Save
B.filename = F{f,3};
if any(strcmpi(B.filename(end-3:end),{'.mgh','.mgz'}))
B.filename = B.filename(1:end-4);
end
palm_miscwrite(B);
end