-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitFile.m
88 lines (72 loc) · 2.23 KB
/
splitFile.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
function outputFilename = splitFile(filename, splitTime, nChans, sr, delOriginal)
% The function splits dat recording file into two separate dat files.
%
% Input: filename should include the full path and the extension of the dat
% file you want to split.
% splitTime is the time in seconds where you want the split to
% occur.
% nChans is the total number of channels in the recording.
% sr is the sampling frequency. The default is 3e4.
% 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 being
% smaller than the original file.
if nargin < 4
sr = 3e4;
end
if nargin < 5
delOriginal = false;
end
chunkSize = 1000000;
fid = []; fidOut = [];
d = dir(filename);
nSampsTotal = d.bytes/nChans/2;
nChunksTotal = ceil(nSampsTotal/chunkSize);
splitChunkSize = splitTime*sr;
splitChunks = ceil(splitChunkSize/chunkSize);
lastSplitChunkSize = round((splitChunkSize - chunkSize*(splitChunks-1)));
nFiles = 2;
try
[pathstr, name, ext] = fileparts(filename);
fid = fopen(filename, 'r');
for iFile = 1:nFiles
outputFilename{iFile} = [pathstr filesep name '_part' 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)
if chunkInd < splitChunks
fwrite(fidOut{1}, dat, 'int16');
elseif chunkInd == splitChunks
fwrite(fidOut{1}, dat(:,1:lastSplitChunkSize), 'int16');
fwrite(fidOut{2}, dat(:,lastSplitChunkSize+1:end), 'int16');
elseif chunkInd > splitChunks
fwrite(fidOut{2}, dat, '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