How to combine and save two XDF files

11 views (last 30 days)
M
M on 29 Oct 2024
Answered: Rahul on 30 Oct 2024
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 Comment
Rahul
Rahul on 29 Oct 2024
Edited: Rahul on 29 Oct 2024
Hi @M,
Can you provide your xdf files which need to be merged, that could help in solving the issue.
Thanks

Sign in to comment.

Answers (2)

Umar
Umar on 29 Oct 2024

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 on 30 Oct 2024
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:

Categories

Find more on EEG/MEG/ECoG 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!