Station 0 · Lesson 3
MATLAB basics for EEGLAB
Your browser's back button takes you to the exact place you were, so open anything you like.
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
What does this show?
Your layout may differ, because MATLAB lets you move the parts around. What matters is finding all three by name.
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.
Why do I need this step?
The data itself lives in MATLAB as a variable called EEG. To read the number of channels or the sampling rate from it, you need only four ideas: the variable, the matrix, the struct 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.
- Open MATLAB and find the three parts: Command Window, Workspace and Current Folder.
- In the Command Window type
x = 5and press Enter. MATLAB prints the result, and the variablexappears in the Workspace. - Type
y = 7;with a semicolon at the end. Nothing is printed, butyappears in the Workspace. The semicolon only stops the printing. - Double-click
xin the Workspace. The Variables editor opens and shows its value. - Open a new script with the New Script button in the Home tab. Paste the script from the next section into it.
- Save it as
demo_basics.min the folder shown in the Current Folder. - Run it with the Run button in the Editor tab, or type its name without
.min 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.
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 do it in EEGLAB?
Here the “GUI” is the MATLAB window itself, not EEGLAB.
- Open MATLAB and find the three parts: Command Window, Workspace and Current Folder.
- In the Command Window type
x = 5and press Enter. MATLAB prints the result, and the variablexappears in the Workspace. - Type
y = 7;with a semicolon at the end. Nothing is printed, butyappears in the Workspace. The semicolon only stops the printing. - Double-click
xin the Workspace. The Variables editor opens and shows its value. - Open a new script with the New Script button in the Home tab. Paste the script from the next section into it.
- Save it as
demo_basics.min the folder shown in the Current Folder. - Run it with the Run button in the Editor tab, or type its name without
.min 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.
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.
% 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 pathWhat each part does:
- struct: the variable
demois a box with fields, each with a name. You reach a field with a dot:demo.srate. TheEEGvariable in EEGLAB is a struct with the same idea, just more fields. - Matrix:
demo.datais 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 asEEG.datain 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
whichcommand tells you where any name will come from. Theaddpathcommand 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.
How do I write it as code?
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 signal. It is called demo so it does not get mixed up with the real EEG variable you will meet in station 1.
% 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 pathWhat each part does:
- struct: the variable
demois a box with fields, each with a name. You reach a field with a dot:demo.srate. TheEEGvariable in EEGLAB is a struct with the same idea, just more fields. - Matrix:
demo.datais a table of numbers. Each row is a channel and each column is a time sample. This is the same order asEEG.datain 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
whichcommand tells you where any name will come from. Theaddpathcommand 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 type | Meaning | Example from the lessons |
|---|---|---|
a = 5 | Create a variable and print its value | x = 5 |
a = 5; | Same, without printing | EEG = pop_eegfiltnew(EEG, 'locutoff', 0.1); |
s.name | A field inside a struct | EEG.srate |
m(2, 1:5) | Elements of a matrix, with round brackets | EEG.data(1, 1:10) |
c{2} | Contents of a cell array element, with curly braces | demo.labels{2} |
'text' | Text in single quotes | 'locutoff' |
% | Everything after it on the line is a comment and is not run | % high-pass |
help name | Help for a function in the Command Window | help pop_eegfiltnew |
Which value should I choose?
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 type | Meaning | Example from the lessons |
|---|---|---|
a = 5 | Create a variable and print its value | x = 5 |
a = 5; | Same, without printing | EEG = pop_eegfiltnew(EEG, 'locutoff', 0.1); |
s.name | A field inside a struct | EEG.srate |
m(2, 1:5) | Elements of a matrix, with round brackets | EEG.data(1, 1:10) |
c{2} | Contents of a cell array element, with curly braces | demo.labels{2} |
'text' | Text in single quotes | 'locutoff' |
% | Everything after it on the line is a comment and is not run | % high-pass |
help name | Help for a function in the Command Window | help pop_eegfiltnew |
How do I check it worked?
5 quick checks
size(demo.data)prints3then1280.demo.srateprints128, anddemo.labels{2}prints'Cz'.t(end)prints9.9922.- A figure window shows a line wiggling around zero for 10 seconds, titled
Channel Cz. which plotprints a path inside the MATLAB installation folder, not inside your own folder.
How do I check it worked?
size(demo.data)prints3then1280.demo.srateprints128, anddemo.labels{2}prints'Cz'.t(end)prints9.9922.- A figure window shows a line wiggling around zero for 10 seconds, titled
Channel Cz. which plotprints 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.datawithout 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.
EEGandeegare different variables. EEGLAB always usesEEGin capitals. - Round brackets with a cell array.
demo.labels(2)gives a cell containing the text, not the text itself. For the text, usedemo.labels{2}. - A script file named after an existing function. If you name your script
plot.moreeglab.m, MATLAB runs your file instead of the original function. Check withwhichif a function behaves strangely.
What mistakes do people make?
- 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.datawithout 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.
EEGandeegare different variables. EEGLAB always usesEEGin capitals. - Round brackets with a cell array.
demo.labels(2)gives a cell containing the text, not the text itself. For the text, usedemo.labels{2}. - A script file named after an existing function. If you name your script
plot.moreeglab.m, MATLAB runs your file instead of the original function. Check withwhichif a function behaves strangely.
Your browser doesn't allow saving progress. All lessons are open, but completion marks won't be saved.
Done. Your progress is saved in this browser.UndoWhere can I read more?
4 sources, official docs first
- EEGLAB TutorialDocumentationReference Topics > MATLAB tutorial
- MATLAB Onramp (MathWorks)MATLAB Onramp
- MATLAB Documentationstruct
- MATLAB DocumentationWhat Is the MATLAB Search Path?
Written for EEGLAB 2026.1 · 2026-10-27
Going further
- EEGLAB Tutorial: https://eeglab.org/tutorials/misc/tutorial_matlab.html (Reference Topics > MATLAB tutorial)
- MATLAB Onramp (MathWorks): https://matlabacademy.mathworks.com/ (MATLAB Onramp)
- MATLAB Documentation: https://www.mathworks.com/help/matlab/ref/struct.html (struct)
- MATLAB Documentation: https://www.mathworks.com/help/matlab/matlab_env/what-is-the-matlab-search-path.html (What Is the MATLAB Search Path?)
Need help?
Ask, share feedback, or report an error
- Report a scientific error · Needs a GitHub account. Don't have one? Use "Share your thoughts".