Station 0 · Lesson 3

MATLAB basics for EEGLAB

Core15 minBefore this lesson: No earlier lesson needed.Print the card

The idea in brief

EEGLAB is written in MATLAB and runs inside it. Every click in EEGLAB’s menus becomes a line of MATLAB behind the scenes, and in station 9 you will learn to collect these lines into a scriptA MATLAB file of commands that runs processing steps in order. that processes every participantThe person whose data were recorded in the study..

Before and after

Schematic of the MATLAB window: Current Folder on the left with the files of the current folder, Command Window in the middle with the line demo.srate = 128 and its output, and Workspace on the right with the variable demo of type 1x1 struct.
What does this show?

Your layout may differ, because MATLAB lets you move the parts around. What matters is finding all three by name.

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 data itself lives in MATLAB as a variable called EEG. To read the number of channels or the sampling rateThe number of samples recorded per second on each channel. from it, you need only four ideas: the variable, the matrix, the structA MATLAB variable that groups several named fields together. and the path.

This lesson is not a MATLAB course. It is the minimum you need to follow the path. If you want more, the official EEGLAB documentation recommends the free MATLAB Onramp course from MathWorks.

How do I do it in EEGLAB?

7 steps in the menus

Here the “GUIThe windows and menus used with the mouse instead of typed commands.” is the MATLAB window itself, not EEGLAB.

  1. Open MATLAB and find the three parts: Command Window, Workspace and Current Folder.
  2. In the Command Window type x = 5 and press Enter. MATLAB prints the result, and the variable x appears in the Workspace.
  3. Type y = 7; with a semicolon at the end. Nothing is printed, but y appears in the Workspace. The semicolon only stops the printing.
  4. Double-click x in the Workspace. The Variables editor opens and shows its value.
  5. Open a new script with the New Script button in the Home tab. Paste the script from the next section into it.
  6. Save it as demo_basics.m in the folder shown in the Current Folder.
  7. Run it with the Run button in the Editor tab, or type its name without .m in the Command Window: demo_basics.
Naming the script file

A script file name starts with a letter and contains only English letters, digits and underscores, with no spaces. Do not give it the name of an existing function such as plot or eeglab.

How do I write it as code?

The same steps in 23 lines of MATLAB

The script builds a small recording by hand: 3 channels for 10 seconds at 128 samples per second, with random numbers instead of a real signalValues that change over time, such as the voltage recorded on a channel.. It is called demo so it does not get mixed up with the real EEG variable you will meet in station 1.

MATLAB
% A toy recording: 3 channels, 10 seconds at 128 Hz
demo.srate  = 128;                              % sampling rate (samples per second)
demo.nbchan = 3;                                % number of channels
demo.pnts   = 10 * demo.srate;                  % samples per channel
demo.labels = {'Fz', 'Cz', 'Pz'};               % channel names (a cell array of text)
demo.data   = randn(demo.nbchan, demo.pnts);    % random numbers, channels x samples

% Read from the struct
size(demo.data)                                 % rows = channels, columns = samples
demo.srate                                      % one field
demo.labels{2}                                  % text of the second channel name

% Indexing: channel 2, samples 1 to 5
firstSamples = demo.data(2, 1:5)

% Time of each sample in seconds (the first sample is at time 0)
t = (0:demo.pnts-1) / demo.srate;
t(end)

% Plot one channel
figure;
plot(t, demo.data(2, :));
xlabel('Time (s)'); ylabel('Amplitude');
title(['Channel ' demo.labels{2}]);

% Where MATLAB finds things
whos demo                                       % size and type of a variable
which plot                                      % location of a function on the path

What each part does:

  • struct: the variable demo is a box with fields, each with a name. You reach a field with a dot: demo.srate. The EEG variable in EEGLAB is a struct with the same idea, just more fields.
  • Matrix: demo.data is a table of numbers. Each row is a channelThe signal recorded from one electrode, as shown in EEGLAB. and each column is a time sampleA single value of the signal at one point in time.. This is the same order as EEG.data in EEGLAB: channels first, then samples.
  • Indexing: demo.data(2, 1:5) means row 2, columns 1 to 5. A colon alone : means “all columns”. Numbering in MATLAB starts at 1, not 0.
  • Time: the first sample is at time zero, so we divide 0 to (number of samples minus 1) by the sampling rate. The last sample is at 9.9922 seconds, not 10.
  • Path: MATLAB finds functions and scripts in the current folder and in a list of folders called the path. The which command tells you where any name will come from. The addpath command adds a folder to the path for this session only. You will use it in the installation lesson in station 1 to add the EEGLAB folder and save it.

Which value should I choose?

Suitable values for your analysis goal

There is no value to choose here, but some similar-looking symbols mean different things. This table is enough to read any line in the EEGLAB lessons.

You typeMeaningExample from the lessons
a = 5Create a variable and print its valuex = 5
a = 5;Same, without printingEEG = pop_eegfiltnew(EEG, 'locutoff', 0.1);
s.nameA field inside a structEEG.srate
m(2, 1:5)Elements of a matrix, with round bracketsEEG.data(1, 1:10)
c{2}Contents of a cell array element, with curly bracesdemo.labels{2}
'text'Text in single quotes'locutoff'
%Everything after it on the line is a comment and is not run% high-pass
help nameHelp for a function in the Command Windowhelp pop_eegfiltnew

How do I check it worked?

5 quick checks
  • size(demo.data) prints 3 then 1280.
  • demo.srate prints 128, and demo.labels{2} prints 'Cz'.
  • t(end) prints 9.9922.
  • A figure window shows a line wiggling around zero for 10 seconds, titled Channel Cz.
  • which plot prints a path inside the MATLAB installation folder, not inside your own folder.

What mistakes do people make?

5 mistakes and how to avoid them
  • Counting from zero. The first element in MATLAB is number 1. Typing demo.data(0, 1) gives an error.
  • Forgetting the semicolon with a large matrix. A line such as x = EEG.data without a semicolon prints millions of numbers and freezes the Command Window. If this happens, press Ctrl+C to stop it.
  • Mixing upper and lower case. EEG and eeg are different variables. EEGLAB always uses EEG in capitals.
  • Round brackets with a cell array. demo.labels(2) gives a cell containing the text, not the text itself. For the text, use demo.labels{2}.
  • A script file named after an existing function. If you name your script plot.m or eeglab.m, MATLAB runs your file instead of the original function. Check with which if a function behaves strangely.

Where can I read more?

4 sources, official docs first

Written for EEGLAB 2026.1 · 2026-10-27

Need help?

Ask, share feedback, or report an error