-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBrainNet_GenCoord.m
62 lines (57 loc) · 1.89 KB
/
BrainNet_GenCoord.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
function BrainNet_GenCoord(filename,outputfilename, regions)
% BrainNet Viewer, a graph-based brain network mapping tool, by Mingrui Xia
% Function to generate node file from voxel-based template
%-----------------------------------------------------------
% Copyright(c) 2015
% State Key Laboratory of Cognitive Neuroscience and Learning, Beijing Normal University
% Written by Mingrui Xia
% Mail to Author: <a href="mingruixia@gmail.com">Mingrui Xia</a>
% Version 1.52;
% Date 20150306;
% Last edited 20150306
%-----------------------------------------------------------
%
% Usage:
% BrainNet_GenCoord(filename,outputfilename);
% filenames is the name of the voxel-based template file.
% outputfilename is the name of the output node file.
% regions is the wanted index in the template.
%
% Example:
%
% Generate node file from AAL.nii template:
% BrainNet_GenCoord('aal.nii','aal.node');
%
% Generate node file from AAL.nii template only with regions 1~10
% BrainNet_GenCoord('aal.nii','aal1-10.node',[1:10]);
[BrainNetPath] = fileparts(which('BrainNet.m'));
BrainNet_SPMPath = fullfile(BrainNetPath, 'BrainNet_spm8_files');
if exist('spm.m','file')
hdr=spm_vol(filename);
vol=spm_read_vols(hdr);
else
addpath(BrainNet_SPMPath);
hdr=BrainNet_spm_vol(filename);
vol=BrainNet_spm_read_vols(hdr);
rmpath(BrainNet_SPMPath);
end
if nargin < 3
index = unique(vol);
index(index == 0) = [];
else
index = regions;
end
coord = zeros(length(index),6);
for i = 1:length(index)
ind = find(vol == index(i));
[x y z] = ind2sub(hdr.dim,ind);
coord_matrix = [mean(x),mean(y),mean(z)];
coord_real = hdr.mat*[coord_matrix,1]';
coord(i,1:3) = coord_real(1:3);
coord(i,6) = index(i);
end
fid = fopen(outputfilename,'wt');
for i=1:length(index)
fprintf(fid,'%f %f %f %f %f %d\n',coord(i,:));
end
fclose(fid);