- Unzip the Files: You can use the MATLAB function “unzip” to do this.
How to analyze EMG signals?
16 views (last 30 days)
Show older comments
My signal datas are in .bin format and all off them are in separate zip files with their corresponding .hea files. How should I import all of these files to MATLAB? And also, should I transform these .bin files to .csv files?
0 Comments
Answers (1)
Raghava S N
on 22 Feb 2024
You can import your “.bin” and “.hea” files using the following steps:
zipFiles = dir('*.zip'); % Adjust the path if needed
for i = 1:length(zipFiles)
unzip(zipFiles(i).name);
end
2. Read the Header Files: The “.hea” files contain metadata about the “.bin” files, such as sampling rate, data format, and channel information.
function metadata = readHeader(headerFile)
fid = fopen(headerFile, 'r');
metadata = struct(); % Initialize a structure to store metadata
% Read the file line by line and parse the information
%% The following is the content in a sample ".hea" file used to test this code.
%%
%%sample.bin
%%1000 1000 16
%%
% This is a simplified example. You'll need to adapt it to your header format
metadata.binFileName = strtrim(fgetl(fid));
% Read the number of samples, sampling rate, and data format
infoLine = fgetl(fid);
info = str2num(infoLine);
metadata.numSamples = info(1);
metadata.samplingRate = info(2);
metadata.dataFormat = 'int16'; % Based on the example header
fclose(fid);
end
3. Import the Binary Data: Once you have the metadata, you can use it to import the binary data correctly. MATLAB's “fread” function can read binary files, but you need to know the data format (e.g., “int16”, “float32”, etc.).
function emgData = importBinaryData(binFile, metadata)
fid = fopen(binFile, 'r');
% Assuming the data is in a specific format and number of channels based on metadata
emgData = fread(fid, [1, metadata.numSamples], metadata.dataFormat);
fclose(fid);
end
4. Combine the Above Steps: You'll want to loop through all your “.hea” and “.bin” files, read the headers, and then import the data accordingly.
headerFiles = dir('*.hea');
for i = 1:length(headerFiles)
metadata = readHeader(headerFiles(i).name);
binFile = strrep(headerFiles(i).name, '.hea', '.bin'); % Assuming bin file has the same name
emgData = importBinaryData(binFile, metadata);
% Now, emgData contains your EMG signals for this recording
% You can process or analyze the data as needed
end
Whether you should convert ".bin" files to ".csv" depends on your needs. Binary files are more compact and faster to read/write for large datasets. However, CSV files are human-readable and can be easily shared and viewed without specialized software.
If you need to share the data with others who do not use MATLAB or if you prefer working with text files, converting to CSV might be beneficial. However, for performance and space efficiency, it's often better to work directly with binary files, especially in MATLAB, which handles binary data well.
If you decide to convert to CSV, you can use MATLAB's “writematrix” function after importing the binary data. Please refer to the following link for the same - https://www.mathworks.com/help/matlab/ref/writematrix.html.
0 Comments
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!