EEGLAB Code for Retaining Online Reference Electrode

Hi EEG researchers,
I am conducting an N400 analysis on EEG data collected with a 32-channel BrainVision system using Cz as the online reference. As a result, Cz is not present in the recorded data. However, because my study requires analyzing the N400 at Cz, I need to reconstruct and retain this channel before re-referencing.
In my dataset, there are 31 scalp electrodes (channels 1–31) and 2 EOG electrodes (channels 32–33). Cz was not recorded as a data channel. In the code below, I manually add Cz back as a new channel (appended as the last channel in chanlocs) using standard location information, and then re-reference the data to the left and right mastoids (channels 10 and 21), excluding the EOG channels.
I would greatly appreciate any feedback on whether this approach is methodologically and technically appropriate. Thank you all!
%% Re-referencing
EEG = eeg_checkset( EEG );
% add Cz back to data
EEG.nbchan = EEG.nbchan + 1;
EEG.data(end+1,:,:) = 0;
EEG.chanlocs(end+1).labels = 'Cz';
tmpl = readlocs('/Users/xxxx/eeglab2025.1.0/plugins/dipfit/standard_BEM/elec/standard_1005.elc');
idxCz = find(strcmpi({tmpl.labels}, 'Cz'), 1);
EEG.chanlocs(end).X = tmpl(idxCz).X;
EEG.chanlocs(end).Y = tmpl(idxCz).Y;
EEG.chanlocs(end).Z = tmpl(idxCz).Z;
EEG.chanlocs(end).theta = tmpl(idxCz).theta;
EEG.chanlocs(end).radius = tmpl(idxCz).radius;
EEG.chanlocs(end).sph_theta = tmpl(idxCz).sph_theta;
EEG.chanlocs(end).sph_phi = tmpl(idxCz).sph_phi;
EEG.chanlocs(end).sph_radius = tmpl(idxCz).sph_radius;
% re-referencing to mastroids, 32 33 are eye electrodes
EEG = pop_reref( EEG, [10 21] ,'exclude',[32 33],'keepref','on'); % still keep the ref channel in data scroll
EEG.setname = 'refed_enc';
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, CURRENTSET + 1,'gui','off');
eeglab redraw;

Answers (1)

Hi Zhuo,
Your approach is generally appropriate for reconstructing an online reference channel in EEGLAB, but a few checks can help ensure the workflow is correct.
  • When Cz is used as the online reference, it is not stored in the recorded data. Adding Cz back as a zero-valued channel before re-referencing is a commonly used method because once the data are re-referenced, the potential at Cz can be reconstructed relative to the new reference.
  • Adding Cz to chanlocs using coordinates from standard_1005.elc is a good practice because it ensures correct spatial information for later analyses such as scalp topographies or source localization.
  • It is recommended to confirm that channels 10 and 21 correspond to the mastoid electrodes in your montage, or use channel labels instead of indices to make the script more robust.
  • After modifying the channel structure, running EEG = eeg_checkset(EEG); again helps ensure the dataset structure remains consistent. Additional details on working with channel structures in MATLAB are available in the MATLAB documentation on structures: https://www.mathworks.com/help/matlab/ref/struct.html.

Categories

Asked:

on 2 Mar 2026 at 23:27

Answered:

ongeveer 9 uur ago

Community Treasure Hunt

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

Start Hunting!