-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRN_customizeTrodesConfig.m
More file actions
executable file
·133 lines (124 loc) · 4.54 KB
/
RN_customizeTrodesConfig.m
File metadata and controls
executable file
·133 lines (124 loc) · 4.54 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
function new_config = RN_customizeTrodesConfig(baseConfig,new_config,nTrodes,spikeThresh,refNTrode,refChan,lfpChan)
% RN_customizeConfig creates a new Trodes config file from an old allowing
% easy editing of settings such as spikeThreshold, reference tetrode,
% reference channel, and lfp channel. For these setting there should be an
% array element for each tetrode in order. Functionality to add tetrodes
% will be coming eventually. If any fields are empty, existing values will
% be used. nTrodes should be a list of tetrode id numbers, if empty or left
% blank, ids will be read from baseConfig
dataFields = {'id','thresh','refNTrode','refChan','lfpChan'};
if nargin<1
[baseConfig,configDir] = uigetfile('.trodesconf','Select base config file');
baseConfig = [configDir baseConfig];
end
if nargin<2
[new_config,configDir2] = uiputfile('.trodesconf','Select output file',[strtok(baseConfig,'.') '_edited']);
new_config = [configDir2 new_config];
end
if ~exist('nTrodes','var')
nTrodes = [];
end
if isempty(nTrodes)
fid = fopen(baseConfig);
nextline = fgets(fid);
while nextline~=-1
id = extractVal(nextline,dataFields{1},'SpikeNTrode');
if ~isempty(id)
nTrodes = [nTrodes id];
end
nextline = fgets(fid);
end
fclose(fid);
end
if nargin<4
spikeThresh = zeros(numel(nTrodes),1);
refNTrode = zeros(numel(nTrodes),1);
refChan = zeros(numel(nTrodes),1);
lfpChan = zeros(numel(nTrodes),1);
currTrode = 0;
fid = fopen(baseConfig);
nextline = fgets(fid);
while nextline~=-1
% Switch Data Fields if using Trodes 1.6 or later
trodesVersion = extractVal(nextline,'trodesVersion','GlobalConfiguration');
if ~isempty(trodesVersion) && ~isempty(regexp(trodesVersion,'1.6.*'))
dataFields = {'id','thresh','refNTrodeID','refChan','LFPChan'};
end
id = extractVal(nextline,dataFields{1},'SpikeNTrode');
thresh = extractVal(nextline,dataFields{2},'SpikeChannel');
if ~isempty(id)
currTrode = find(nTrodes==id);
refNTrode(currTrode) = extractVal(nextline,dataFields{3},'SpikeNTrode');
refChan(currTrode) = extractVal(nextline,dataFields{4},'SpikeNTrode');
lfpChan(currTrode) = extractVal(nextline,dataFields{5},'SpikeNTrode');
elseif ~isempty(thresh)
spikeThresh(currTrode) = thresh;
end
nextline = fgets(fid);
end
fclose(fid);
end
TrodePrefs = [];
for i=1:numel(nTrodes),
TrodePrefs = [TrodePrefs struct('id',nTrodes(i),'lfpChan',lfpChan(i),...
'refNTrode',refNTrode(i),'refChan',refChan(i),'thresh',spikeThresh(i))];
end
TrodePrefs2 = RN_extractionTrodesConfGUI(TrodePrefs);
if isempty(TrodePrefs2)
disp('New Config Cancelled...base config returned.')
new_config = baseConfig;
return;
end
fid = fopen(baseConfig);
fid2 = fopen(new_config,'w');
nextline = fgets(fid);
while nextline~=-1
outLine = nextline;
% Switch Data Fields if using Trodes 1.6 or later
trodesVersion = extractVal(nextline,'trodesVersion','GlobalConfiguration');
if ~isempty(trodesVersion) && ~isempty(regexp(trodesVersion,'1.6.*'))
dataFields = {'id','thresh','refNTrodeID','refChan','LFPChan'};
end
id = extractVal(nextline,dataFields{1},'SpikeNTrode');
thresh = extractVal(nextline,dataFields{2},'SpikeChannel');
if ~isempty(id)
n = find(nTrodes==id);
outLine = replaceVal(outLine,dataFields{5},'SpikeNTrode',TrodePrefs2(n).lfpChan);
outLine = replaceVal(outLine,dataFields{4},'SpikeNTrode',TrodePrefs2(n).refChan);
outLine = replaceVal(outLine,dataFields{3},'SpikeNTrode',TrodePrefs2(n).refNTrode);
elseif ~isempty(thresh)
outLine = replaceVal(outLine,dataFields{2},'SpikeChannel',TrodePrefs2(n).thresh);
end
if isempty(outLine)
disp('Unable to correct config file...Fields to replace do not exist');
outLine = nextline;
end
fprintf(fid2,'%s',outLine);
nextline = fgets(fid);
end
fclose(fid);
fclose(fid2);
function out = extractVal(str,tag,lineStart)
out = [];
a = strfind(str,tag);
b = strfind(str,lineStart);
if isempty(a) || isempty(b)
return;
end
a = a + numel(tag) + 2;
b = a+find(str(a:end)=='"',1,'first')-2;
str = str(a:b);
out = str2double(str);
if isnan(out)
out = str;
end
function out = replaceVal(str,tag,lineStart,newVal)
out = [];
a = strfind(str,tag);
b = strfind(str,lineStart);
if isempty(a) || isempty(b)
return;
end
a = a + numel(tag) + 2;
b = a+find(str(a:end)=='"',1,'first')-2;
out = [str(1:a-1) num2str(newVal) str(b+1:end)];