-
Notifications
You must be signed in to change notification settings - Fork 0
/
reorderChannels.m
64 lines (50 loc) · 1.48 KB
/
reorderChannels.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 outputFilename = reorderChannels(filename, nChans, newOrder, delOriginal)
% The function reorders channels in the recording producing a new dat file.
%
% Input: filename should include the full path and the extension.
% nChans is the total number of channels in the recording.
% newOrder is the vector containing the new channel order.
% delOriginal is true if the original file is to be deleted.
% Otherwise it is false.
%
% Output: outputFilename is the name of the data file with fewer channels.
chunkSize = 1000000;
fid = []; fidOut = [];
d = dir(filename);
nSampsTotal = d.bytes/nChans/2;
nChunksTotal = ceil(nSampsTotal/chunkSize);
try
[pathstr, name, ext] = fileparts(filename);
fid = fopen(filename, 'r');
if isempty(pathstr)
outputFilename = [name '_reordered' ext];
else
outputFilename = [pathstr filesep name '_reordered' ext];
end
fidOut = fopen(outputFilename, 'w');
chunkInd = 1;
while 1
fprintf(1, 'chunk %d/%d\n', chunkInd, nChunksTotal);
dat = fread(fid, [nChans chunkSize], '*int16');
if ~isempty(dat)
dat = dat(newOrder,:);
fwrite(fidOut, dat, 'int16');
else
break
end
chunkInd = chunkInd+1;
end
fclose(fid);
fclose(fidOut);
if delOriginal
delete(filename);
end
catch me
if ~isempty(fid)
fclose(fid);
end
if ~isempty(fidOut)
fclose(fidOut);
end
rethrow(me)
end