Repository navigation
Expand file tree
/
Copy pathsplitDatFile.m
More file actions
64 lines (54 loc) · 2.32 KB
/
Copy pathsplitDatFile.m
File metadata and controls
64 lines (54 loc) · 2.32 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
function splitDatFile(datFilePath, outputDir)
% splitDatFile - Split a large .dat file into separate files, one per channel.
%
% This function reads a .dat file and its associated .xml metadata file.
% It then splits the data into individual channels and saves each channel's
% data as a separate binary (.bin) or MATLAB (.mat) file.
%
% Inputs:
% datFilePath - Path to the .dat file.
% outputDir - Directory where individual channel files will be saved.
%
% Example:
% splitDatFile('data.dat', 'outputChannels');
%
% Author: Your Name
% Date: YYYY-MM-DD
%% Parse File and Metadata
[dataDir, baseName, ext] = fileparts(datFilePath);
if ~strcmp(ext, '.dat')
error('Provided file must be a .dat file.');
end
xmlFilePath = fullfile(dataDir, [baseName '.xml']);
if ~exist(xmlFilePath, 'file')
error('The corresponding .xml file does not exist: %s', xmlFilePath);
end
% Extract metadata from .xml
doc = xmlread(xmlFilePath);
nChannels = str2double(char(doc.getElementsByTagName('nChannels').item(0).getFirstChild.getData));
samplingRate = str2double(char(doc.getElementsByTagName('samplingRate').item(0).getFirstChild.getData));
% Validate output directory
if ~exist(outputDir, 'dir')
mkdir(outputDir);
end
%% Memory Map the File
fileInfo = dir(datFilePath);
totalSamples = fileInfo.bytes / (2 * nChannels); % int16 data, 2 bytes per sample
m = memmapfile(datFilePath, 'Format', 'int16');
%% Split Data into Individual Channel Files
fprintf('Splitting %d channels...\n', nChannels);
for ch = 1:nChannels
% Extract data for the current channel
channelData = double(m.Data(ch:nChannels:end));
% Option 1: Save as binary file
binFileName = fullfile(outputDir, sprintf('%s_channel%d.bin', baseName, ch));
fileID = fopen(binFileName, 'w');
fwrite(fileID, channelData, 'double');
fclose(fileID);
% Option 2: Save as MATLAB .mat file (optional, larger files)
% matFileName = fullfile(outputDir, sprintf('%s_channel%d.mat', baseName, ch));
% save(matFileName, 'channelData', '-v7.3'); % Use -v7.3 for large arrays
fprintf('Channel %d saved to %s\n', ch, binFileName);
end
fprintf('Splitting complete. Data saved in %s.\n', outputDir);
end