-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntools_elec_calc_grid.m
121 lines (100 loc) · 3.56 KB
/
ntools_elec_calc_grid.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function [elec, elec_stats, info_cell, data]= ntools_elec_calc_grid(ini_cell, subjectpath,scale,radius)
% calculate the grid electrodes with initial locations, ini_cell is a cell
% array in which first column is the [elec_name number], the rest 3
% columns are the x y z coordinates
%
% output: elec is a structure contains the grid names and their locations,
% data are only the coordinates without grid names, used for binary image
% conversion
if isempty(ini_cell)
disp('No grid electrode.');
elec = []; data = []; elec_stats = []; info_cell = [];
return;
end
fprintf('Calculating the grids.....\n')
name = regexpi(ini_cell(:,1),'[A-Za-z]*[^\d*]','match');
type = upper(ini_cell(:,5));
for i=1:length(name)
ini_name(i) = name{i};
end
[name,ia] = unique(ini_name,'stable');
type = type(ia);
% get the grid initial positions by name
for i = 1:length(name)
elec_temp = cell(size(ini_cell));
% n = strfind(ini_cell(:,1),name{i});
n = regexpi(ini_cell(:,1),['^' name{i} '[\d]'],'match');
k = 1;
for l = 1:length(n)
if ~isempty(n{l})
elec_temp(k,:) = ini_cell(l,:);
k = k+1;
end
end
elec_temp(all(cellfun(@isempty,elec_temp),2),:) = [];
elec_num = regexpi(elec_temp(:,1),'[^A-Za-z]*[\d*]','match');
elec_num(all(cellfun(@isempty,elec_num),2),:) = [];
% if length(elec_num)~=2
% error('only 2 initial positions are required');
% end
% ini_pos = [cell2num(elec_num{1}); cell2num(elec_num{2})];
for ll = 1:length(elec_num)
ini_pos(ll) = str2double(cell2mat(elec_num{ll}));
end
ini_loc = cell2mat(elec_temp(:,2:4));
% determine the hemisphere that grid locates
if ini_loc(:,1)>0
sph = 'rh';
elseif ini_loc(:,1)<0
sph = 'lh';
else
error(['wrong initial positions for grid ', name{i}]);
end
% input the size of the grid
if ~isequal('MG',type{i})
a = menu(['What is the size of the grid ',name{i},' ?'], '8*8', '4*8', '4*5','2*8','manually input');
if a==1
s = [8,8];
elseif a==2
s = [4,8];
elseif a==3
s = [4,5];
elseif a==4
s = [2,8];
else
s = input(['Please input the size of the grid ',name{i},' [row column]: ']);
end
else
s = [8,8]; % for PMT model 2110-128-021 only 8*8 is supported
radius = 10;
end
% check for radius
if isempty(radius)
radius = input('\nInput the inter-electrode distance (mm) (Press Enter for default distance 10mm) : ');
if isempty(radius), radius = 10; end
end
% project the grid on the outer-brain surface
[elec_proj, info_cell]= ntools_elec_projection(ini_loc,ini_pos,s(1),s(2),sph,subjectpath,scale,radius);
elec_temp2.(char(name{i})) = elec_proj{1,5};
elec_stats(i,:) = elec_proj(1,1:4);
% clear the unnecessary data
radius = []; % reset radius for next grid
clear elec_temp elec_num elec_proj ini_pos ini_loc;
fprintf('Grid stats: itr#: %f mean: %f std: %f\n\n',cell2mat(elec_stats(i,1:3)))
end
%% get the data from the struct
l = 1;
for i = 1:length(name)
for j = 1:size(elec_temp2.(char(name{i})),1)
name_num(l) = cellstr(sprintf('%s%.2d',char(name{i}),j));
elec_pos(l,:) = elec_temp2.(char(name{i}))(j,:);
elec_type(l) = type(i);
l = l+1;
end
end
elec = cell(size(name_num,2),5);
elec(:,1) = upper(name_num)';
elec(:,2:4) = num2cell(elec_pos);
elec(:,5) = elec_type;
elec = ntools_elec_calc_mesogrid(elec,subjectpath);
fprintf('Done \n\n');