Intro to MATLAB · for EEGLAB lesson 6 / 8

Structs and the EEG Variable

A struct is one variable with named parts, and EEG is a struct.

1Fields

A struct keeps related values together under one name. Each value sits in a named field, reached with a dot.

info.subject = 'sub-01';
info.srate = 500;
info.condition = 'oddball';
info
info.srate
fieldnames(info)

2Loading a dataset

EEGLAB is already on the path and started in this lesson, and the current folder is psych390. pop_loadset reads a dataset from disk and hands it back as a struct, stored here in EEG.

EEG = pop_loadset('filename', 'sub-01.set', 'filepath', 'data');
EEG

Typing EEG lists every field with its value, or its size when the value is too big to show.

3The fields you will use most

EEG.srate  # samples per second
EEG.nbchan  # number of channels
EEG.pnts  # time points per epoch (the whole recording, before epoching)
EEG.pnts / EEG.srate  # seconds of recording
size(EEG.data)

EEG.data has 32 rows, one per channel, and 30,000 columns: 60 seconds times 500 samples per second. After the data are cut into epochs it gains a third dimension, one layer per trial, and EEG.trials counts them.

4Indexing into EEG.data

This is the matrix indexing from the last lesson, on real-sized data. The voltages are in microvolts; EEG.times gives the time of each column in milliseconds.

EEG.data(14, 1:5)  # channel 14, first five samples
EEG.times(1:5)
mean(EEG.data(14, :))

5Structs inside structs

Some fields hold struct arrays, one element per channel or per event. Pick the element with parentheses, then the field with a dot.

EEG.chanlocs
EEG.chanlocs(14).labels
EEG.event(1)
EEG.event(1).type
numel(EEG.event)

Leave out the index and you ask for all 32 labels at once. MATLAB prints them one after another; to keep them as a list, wrap the request in braces.

labels = {EEG.chanlocs.labels}

6Changing a field

EEG.setname = 'sub-01 practice';
EEG.setname

This changes the copy in memory only. The file on disk stays the same until you save it with pop_saveset (lesson 8).

In EEGLAB. The main EEGLAB window shows the dataset name, the number of channels, the frames per epoch, the number of events, and the sampling rate. Those come straight from EEG.setname, EEG.nbchan, EEG.pnts, EEG.event, and EEG.srate.

Your turn

Each box ticks itself when you do it in the practice window. Stuck? "show me" puts a command in the window for you.

References

The MathWorks, Inc. (2025). MATLAB documentation. https://www.mathworks.com/help/matlab/

EEGLAB Wiki. (n.d.). EEGLAB data structures. https://eeglab.org/tutorials/ConceptsGuide/Data_Structures.html

Delorme, A., & Makeig, S. (2004). EEGLAB: An open source toolbox for analysis of single-trial EEG dynamics including independent component analysis. Journal of Neuroscience Methods, 134(1), 9 to 21. https://doi.org/10.1016/j.jneumeth.2003.10.009