-
Notifications
You must be signed in to change notification settings - Fork 20
/
cifti_struct_dense_replace_volume_structure_data.m
64 lines (64 loc) · 2.61 KB
/
cifti_struct_dense_replace_volume_structure_data.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
function cifti = cifti_struct_dense_replace_volume_structure_data(cifti, data, structure, cropped, dimension)
%function cifti = cifti_struct_dense_replace_volume_structure_data(cifti, newdata, structure, cropped, dimension)
% Replace the data in a single cifti volume structures, taking a 4D array as input.
% For a single-map cifti, the input can be 3D instead.
%
% The cropped argument is optional and defaults to false, expecting a volume with
% the full original dimensions.
% The dimension argument is optional except for dconn files (generally, use 2 for dconn).
% The cifti struct must have exactly 2 dimensions.
if length(cifti.diminfo) < 2
error('cifti struct must have 2 dimensions');
end
if length(cifti.diminfo) > 2
error('this function only operates on 2D cifti, use cifti_diminfo_dense_get_volume_structure_info instead');
end
sanity_check_cdata(cifti);
if nargin < 4
cropped = false;
end
if nargin < 5
dimension = [];
for i = 1:2
if strcmp(cifti.diminfo{i}.type, 'dense')
dimension = [dimension i]; %#ok<AGROW>
end
end
if isempty(dimension)
error('cifti struct has no dense dimension');
end
if ~isscalar(dimension)
error('dense by dense cifti (aka dconn) requires specifying the dimension argument');
end
end
otherdim = 3 - dimension;
otherlength = size(cifti.cdata, otherdim);
volinfo = cifti_diminfo_dense_get_volume_structure_info(cifti.diminfo{dimension}, structure, cropped);
indlist = cifti_vox2ind(volinfo.voldims, volinfo.voxlist1);
datadims = size(data);
if length(datadims) < 4
if otherlength ~= 1 || length(datadims) < 3
error('data must have 4 dimensions (or 3 for a single-map cifti)');
end
datadims = [datadims 1];
end
if datadims(1:3) ~= volinfo.voldims
error('input data has the wrong volume dimensions, check the "cropped" argument');
end
if datadims(4) ~= otherlength
error('input data has the wrong number of frames');
end
if otherlength == 1 %don't loop if we don't need to
cifti.cdata(volinfo.ciftilist) = data(indlist);
else
%have a dimension that goes after the ind2sub result, so loop
for i = 1:otherlength
tempframe = data(:, :, :, i);
if dimension == 1
cifti.cdata(volinfo.ciftilist, i) = tempframe(indlist);
else
cifti.cdata(i, volinfo.ciftilist) = tempframe(indlist);
end
end
end
end