How to combine and save two XDF files

조회 수: 11 (최근 30일)
M
M 2024년 10월 29일
답변: Rahul 2024년 10월 30일
Unfortunately, the EEG recording of one participant was interrupted and now I have two XDF files that I would like to combine. Could someone please share how I can merge these two files (with 3 streams each) without losing any information? Thanks a lot in advance!
  댓글 수: 1
Rahul
Rahul 2024년 10월 29일
편집: Rahul 2024년 10월 29일
Hi @M,
Can you provide your xdf files which need to be merged, that could help in solving the issue.
Thanks

댓글을 달려면 로그인하십시오.

답변 (2개)

Umar
Umar 2024년 10월 29일

Hi @M,

you can utilize the xdf library, which is specifically designed for handling XDF files. Below is a step-by-step approach to combine the two files while preserving all streams, try using the xdf.loadXDF function to load both XDF files into MATLAB. Concatenate the streams from both files. Ensure that the timestamps are adjusted accordingly to maintain the integrity of the data. Use the xdf.saveXDF function to save the combined streams into a new XDF file. Here is a sample code snippet to illustrate the process:

% Load the XDF files
[streams1, header1] = xdf.loadXDF('file1.xdf');
[streams2, header2] = xdf.loadXDF('file2.xdf');
% Combine streams
combinedStreams = [streams1; streams2];
% Save the merged file
xdf.saveXDF('merged_file.xdf', combinedStreams);

For more guidance, please review the link provided below.

https://github.com/xdf-modules/xdf-Matlab#

Hope this helps.


Rahul
Rahul 2024년 10월 30일
Hi M,
I understand that you are trying to combine and save two different EEG data XDF files, without losing any information.
Currently, there is no direct functionality or existing workaround for the this issue. Although, you can leverage ‘xdf-MATLAB’ library, which provides support tools for MATLAB, to work with xdf files or eeg data. You can use the ‘load_xdf’ functions for MATLAB to read and manipulate XDF files through its Lab Streaming Layer (LSL).
Merging two XDF files in MATLAB can be done by first loading the XDF files, identifying corresponding streams, and then concatenating the time series data and timestamps for each stream. Here’s how you can structure your code to achieve the same:
% Load two XDF files
[streams1, ~] = load_xdf('file1.xdf');
[streams2, ~] = load_xdf('file2.xdf');
% Initialize cell array to hold combined streams
combined_streams = streams1; % Start with streams1 structure
% Iterate over each stream in the first file
for i = 1:length(streams1)
% Find the matching stream in the second file
for j = 1:length(streams2)
if strcmp(streams1{i}.info.name, streams2{j}.info.name) % Match streams by name
% Concatenate time series data and timestamps from both files
combined_streams{i}.time_series = [streams1{i}.time_series, streams2{j}.time_series];
combined_streams{i}.time_stamps = [streams1{i}.time_stamps, streams2{j}.time_stamps];
% Sort by timestamp to ensure chronological order
[combined_streams{i}.time_stamps, sort_idx] = sort(combined_streams{i}.time_stamps);
combined_streams{i}.time_series = combined_streams{i}.time_series(:, sort_idx); % Adjust series order
end
end
end
% Save combined data as a .mat file
save('combined_data.mat', 'combined_streams');
For more information regarding ’xdf-MATLAB’ library and its supported tools for working with XDF files, refer to the link mentioned below:

카테고리

Help CenterFile Exchange에서 EEG/MEG/ECoG에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by