Station 1 · Lesson 6

Merging & selecting data

Recommended12 minBefore this lesson: ALLEEG & dataset managementPrint the card
Starts from: eeglab_data.set (D1) · Nothing yet: the data as downloaded.Download the data (opens in a new tab)
Script to reach this state
MATLAB
[ALLEEG, EEG, CURRENTSET, ALLCOM] = eeglab;
sampleFolder = fullfile(fileparts(which('eeglab')), 'sample_data');
EEG = pop_loadset('filename', 'eeglab_data.set', 'filepath', sampleFolder);
[ALLEEG, EEG, CURRENTSET] = pop_newset(ALLEEG, EEG, 0, 'setname', 'Continuous EEG Data', 'gui', 'off');
eeglab redraw;

The idea in brief

Many experiments record each participantThe person whose data were recorded in the study. in several runs, with a break in between. To analyze them together, you merge them into one datasetOne EEG data file loaded in EEGLAB together with its information.. Sometimes you need the opposite: only one part of the recording. But how does EEGLAB know that the signalValues that change over time, such as the voltage recorded on a channel. was broken at the join?

Before and after

A faded time bar of the whole of D1, with scissors and an arrow labelled Select data leading to two bars: D1 part 1 from 0 to 120 s (15361 samples) and D1 part 2 from 120 to 238.3 s (15143 samples), with a small gap between them. Then an arrow labelled Append datasets leads to one bar, D1 merged (30504 samples), with a thick red vertical line labelled boundary at the join.

Your next questions, each answered in its own section:

Why do I need this step?

The reasons in detail, and the terms you need

The answer is a special eventA marker in the recording that flags a moment of interest, such as a stimulus. called boundary. EEGLAB adds it automatically at every join or cut, and later filteringApplying a filter to the data. and epoching respect it. So that you need no new data, we practice on eeglab_data.set: we split it into two parts, merge them again, and see where the boundary event appears.

In real experiments, each participant is often recorded in several runs. For example, the raw version of the station 8 data on OpenNeuro (ds002680) has two sessions of 12 or 13 runs for each participant. The ready copy we use in station 8 has already been prepared, with one file per conditionAn experimental situation compared with others, such as familiar vs unfamiliar..

How do I do it in EEGLAB?

23 steps in the menus

1. Cut the first part

  1. With eeglab_data.set open as dataset 1, open Edit then Select data.
  2. In the Time range [min max] (s) row, type 0 120. Leave the checkbox under on->remove these in the same row unticked.
  3. Click OK. In the What do you want to do with the new dataset? window, type D1 part 1 in Name it:, then click OK.
  4. The first line of the main window is now #2: D1 part 1, and Frames per epoch shows 15361.

2. Cut the second part

  1. From the Datasets menuA list of commands that opens from its name at the top of the window., choose Dataset 1:Continuous EEG Data. Always cut from the original, not from part 1.
  2. Open Edit then Select data again.
  3. Type the same range 0 120 in Time range [min max] (s), and this time tick the checkbox under on->remove these. EEGLAB now removes the first 120 seconds and keeps the rest.
  4. Click OK, and name the new dataset D1 part 2.
  5. The main window shows #3: D1 part 2 and 15143 samples.

Using the same range with and without the checkbox makes sure that the two parts do not overlap and that no sampleA single value of the signal at one point in time. is lost between them.

3. Merge the two parts

  1. Open Edit then Append datasets.
  2. In Dataset indices to merge, type 2 3. The order matters: the first dataset comes first in time.
  3. Leave Preserve ICA weights of the first dataset ? unticked, and click OK.
  4. EEGLAB suggests the name Merged datasets. Change it to D1 merged, then click OK.
  5. The main window shows #4: D1 merged: 30504 samples like the original, and Events shows 155 instead of 154.

The Command Window may print Warning: duplicate boundary event removed. This is normal: each part already had a boundary at its cut edge, and EEGLAB keeps only one at the join.

4. Find the join

  1. With D1 merged as the current dataset, open Plot then Channel data (scroll).
  2. In the box between the two arrows at the bottom of the window, type 118 and press Enter.
  3. At 120 seconds you see a thick red line labelled boundary. This is the extra event: number 155.

5. Select the data around one event type

  1. Go back to Dataset 1:Continuous EEG Data in the Datasets menu.
  2. Open Edit then Select data using events. The window Remove data portions around events - pop_rmdat() opens.
  3. In Event type(s) ([]=all), type square.
  4. In Time limits [start, end] in sec., type -0.5 1: from half a second before each square event to one second after it.
  5. Leave the list on Keep selected, and click OK. Name the dataset D1 around square.
  6. 15336 samples remain (about 120 seconds), and Events shows 234: all 154 original events, plus 80 boundary events, one at each removed part.

How do I write it as code?

The same steps in 29 lines of MATLAB
MATLAB
% Start EEGLAB and load the sample dataset as dataset 1
[ALLEEG, EEG, CURRENTSET, ALLCOM] = eeglab;
sampleFolder = fullfile(fileparts(which('eeglab')), 'sample_data');
EEG = pop_loadset('filename', 'eeglab_data.set', 'filepath', sampleFolder);
[ALLEEG, EEG, CURRENTSET] = pop_newset(ALLEEG, EEG, 0, ...
    'setname', 'Continuous EEG Data', 'gui', 'off');

% Part 1: keep the first 120 seconds (dataset 2)
EEG = pop_select(ALLEEG(1), 'time', [0 120]);
[ALLEEG, EEG, CURRENTSET] = pop_newset(ALLEEG, EEG, CURRENTSET, ...
    'setname', 'D1 part 1', 'gui', 'off');

% Part 2: remove the same 120 seconds and keep the rest (dataset 3)
EEG = pop_select(ALLEEG(1), 'rmtime', [0 120]);
[ALLEEG, EEG, CURRENTSET] = pop_newset(ALLEEG, EEG, CURRENTSET, ...
    'setname', 'D1 part 2', 'gui', 'off');

% Append datasets 2 and 3 (dataset 4)
EEG = pop_mergeset(ALLEEG, [2 3], 0);
[ALLEEG, EEG, CURRENTSET] = pop_newset(ALLEEG, EEG, CURRENTSET, ...
    'setname', 'D1 merged', 'gui', 'off');

[ALLEEG.pnts]                                  % 30504 15361 15143 30504
isequal(ALLEEG(4).data, ALLEEG(1).data)        % 1: same signal as the original
eeg_eventtypes(EEG)                            % square 80, rt 74, boundary 1
EEG.event(eeg_findboundaries(EEG)).latency     % 15361.5: between samples 15361 and 15362

% Keep only the data from -0.5 to 1 s around each square event (dataset 5)
EEG = pop_rmdat(ALLEEG(1), {'square'}, [-0.5 1], 0);
[ALLEEG, EEG, CURRENTSET] = pop_newset(ALLEEG, EEG, CURRENTSET, ...
    'setname', 'D1 around square', 'gui', 'off');
EEG.pnts                                       % 15336
numel(eeg_findboundaries(EEG))                 % 80
eeglab redraw;
  • 'time' keeps the range, and 'rmtime' removes it. They are the checkbox under on->remove these, unticked and ticked.
  • pop_mergeset(ALLEEG, [2 3], 0) merges dataset 2, then 3. The final zero means: do not keep the ICAA method that separates the recording into independent sources, some brain and some artifact. weights.
  • The last number in pop_rmdat is the list in the window: 0 means Keep selected, and 1 means Remove selected.

Which value should I choose?

Suitable values for your analysis goal
What you wantMenuWhat to type
One continuous part of the recordingEdit then Select dataThe range in seconds in Time range [min max] (s), checkbox unticked.
Remove one continuous part (start, end or a break)Edit then Select dataThe same range, with the checkbox under on->remove these ticked.
The data around one event typeThe name or code that identifies what kind of event it is.Edit then Select data using eventsThe event type, the limits in seconds around it, and Keep selected.
Remove the data around an event, such as a TMS pulse that saturates the amplifierEdit then Select data using eventsThe event type, short limits around it, and Remove selected. The official tutorial gives this example.
Several runs of one participant in one datasetEdit then Append datasetsThe dataset numbers in the order they were recorded.

When to merge? Usually right after import and channelThe signal recorded from one electrode, as shown in EEGLAB. locations, before filtering and ICA. ICA needs a lot of data, and the runs of one session share the same electrodes.

How do I check it worked?

5 quick checks
  1. The Datasets menu lists five datasets: Continuous EEG Data, D1 part 1, D1 part 2, D1 merged and D1 around square.
  2. Frames per epoch for the first four: 30504, 15361, 15143 and 30504.
  3. D1 merged has 155 events, eeg_eventtypes shows boundary 1, and you see it as a red line at 120 seconds in the scroll window.
  4. isequal(ALLEEG(4).data, ALLEEG(1).data) gives 1: merging gave back exactly the original signal.
  5. D1 around square has 15336 samples and 80 boundary events.

Nothing in this lesson is saved to disk. Delete datasets 2 to 5 from memory with Edit then Delete dataset(s) from memory before the next lesson, or close EEGLAB.

The Methods sentence below is for real use, when you merge several runs of one participant, not for the two-halves exercise in this lesson.

What mistakes do people make?

5 mistakes and how to avoid them
  • Merging datasets with a different number of channels or sampling rateThe number of samples recorded per second on each channel.. Edit then Append datasets stops with the error The two datasets must have the same number of channels or The two datasets must have the same sampling rate. Make channels and rate match before merging.
  • Merging datasets with the same number of channels in a different order. EEGLAB checks the number only, not the names. The Cz signal of one run can end up under the name Pz of another, with no warning. Compare {EEG.chanlocs.labels} in both datasets before merging.
  • Deleting or ignoring boundary events in an old script. EEGLAB filters each part between two boundary events on its own, and epoching skips any epoch that crosses a join. If you delete them, or filter EEG.data directly with a MATLAB function, the join is treated as a continuous signal and a false jump appears.
  • Forgetting that every cut and every merge is a new dataset. Edit then Select data and Edit then Append datasets add a new dataset to ALLEEG, and the original stays as it is. Give each copy a clear name, and look at the first line of the main window before each step.
  • Cutting from part 1 instead of the original. In step 2, if D1 part 1 is still the current dataset, you remove the first 120 seconds of it, and almost nothing is left. Go back to dataset 1 first.

How do I write it in my paper?

A Methods template: edit the data, settings and versions to match what you ran

A template: edit the data, settings and versions to match what you ran.

Data from the [N] recording runs were concatenated into a single continuous dataset (pop_mergeset, EEGLAB 2026.1), with boundary events marking the joins.

Where can I read more?

4 sources, official docs first

Written for EEGLAB 2026.1 · 2026-10-28

Need help?

Ask, share feedback, or report an error