-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitChannels.m
76 lines (61 loc) · 1.88 KB
/
splitChannels.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
function outputFilename = splitChannels(filename, nChans, chans2split, delOriginal)
% The function splits channels into separate dat files. It should be used
% when more than one probe was used during the recording. The files are
% then processed separately in the spikeSortingPipeline.
%
% Input: data filename should include the full path and the extension.
% nChans is the total number of channels in the recording.
% chans2split is a cell array of channels corresponding to separate
% files.
% delOriginal is true if the original file is to be deleted.
% Otherwise it is false.
%
% Output: outputFilename is the cell array with output filenames all having
% fewer channels than the original file.
if nargin < 4
delOriginal = false;
end
chunkSize = 1000000;
fid = []; fidOut = [];
d = dir(filename);
nSampsTotal = d.bytes/nChans/2;
nChunksTotal = ceil(nSampsTotal/chunkSize);
nFiles = numel(chans2split);
try
[pathstr, name, ext] = fileparts(filename);
fid = fopen(filename, 'r');
for iFile = 1:nFiles
outputFilename{iFile} = [pathstr filesep name '_probe' num2str(iFile) ext]; %#ok<*AGROW>
fidOut{iFile} = fopen(outputFilename{iFile}, 'w');
end
chunkInd = 1;
while 1
fprintf(1, 'chunk %d/%d\n', chunkInd, nChunksTotal);
dat = fread(fid, [nChans chunkSize], '*int16');
if ~isempty(dat)
for iFile = 1:nFiles
fwrite(fidOut{iFile}, dat(chans2split{iFile},:), 'int16');
end
else
break
end
chunkInd = chunkInd+1;
end
fclose(fid);
for iFile = 1:nFiles
fclose(fidOut{iFile});
end
if delOriginal
delete(filename);
end
catch me
if ~isempty(fid)
fclose(fid);
end
if ~isempty(fidOut)
for iFile = 1:nFiles
fclose(fidOut{iFile});
end
end
rethrow(me)
end