Adding Event Markers to Imported EEG Data - Stacked Plot

24 views (last 30 days)
I am currently working with Matlab to analyze EEG data from an Emotiv EPOC. I have it all imported and I have several plots made from the csv files. However, is there any way I can add (preferably manually) event markers to these plots? I have a stacked plot with all channels individually shown, a figure with all channels shown in tiled layout, and a plot with all channels shown. I've included the code below and attached the relating data files. I'm grateful for any help you can offer!
%% Importing_and_Plotting_EEG_Example
close all; clear all; clc;
% look to 300 ms for peak after stimulation
% ### EDIT: I commented out the next 4 lines so your code can be run in Answers
% % add folder with csv file to path
% addpath 'C:\Users\chris\Documents\College\Sixth Year\DAGSI\Data\EMOTIV Recordings\Event Marker Try 4'
% % change current directory to where this Matlab file is
% cd 'C:\Users\chris\Documents\College\Sixth Year\DAGSI\Data\EMOTIV Recordings'
% stops Matlab from rounding numbers
format long;
% Importing csv data
T=readtable('Try 4.csv'); %This reads the csv data into a table named T
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
timestampOrig=T.Timestamp; %this makes timestampOrig equal to the original Timestamp
T.Timestamp=T.Timestamp-T.Timestamp(1); %This changes the official Timestamp column so it is 0 at the start of the recording
% Changes the sensor names from EEG_<sensor> to <sensor>
T5=head(T, 5) %shows first 5 rows of table T
T5 = 5×15 table
Timestamp AF3 F7 F3 FC5 T7 P7 O1 O2 P8 T8 FC6 F4 F8 AF4 ___________________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ 0 4260.384766 4267.692383 4316.025879 4305.512695 4340 4278.461426 4278.974121 4279.615234 4235.256348 4184.871582 4456.666504 4401.794922 4343.589844 4387.05127 0.00770998001098633 4264.871582 4261.922852 4311.794922 4308.589844 4338.974121 4280.256348 4279.743652 4276.794922 4232.179688 4184.743652 4442.692383 4401.538574 4334.230957 4380.769043 0.0155198574066162 4266.282227 4272.05127 4316.410156 4312.563965 4340.512695 4280.769043 4286.410156 4275.769043 4229.615234 4178.333496 4438.205078 4405.384766 4325.256348 4381.025879 0.0232298374176025 4262.820313 4274.743652 4319.743652 4311.282227 4342.436035 4280.769043 4288.205078 4281.410156 4242.820313 4181.282227 4444.871582 4407.820313 4330.641113 4389.487305 0.0310399532318115 4266.153809 4267.563965 4318.846191 4308.077148 4332.563965 4279.230957 4282.307617 4285.641113 4248.589844 4186.025879 4448.717773 4404.230957 4333.717773 4392.307617
%% Stacked Plot
% Plot all electrode readings on the same plot (vs timestamp)
fig0=figure('Name', 'EEG Signals - Stacked Plot');
%creates a stacked plot with Timestamp as x-axis and the values of the variables in
%columns 2:end as the y-axis
grid on; xlabel('Timestamp (s)'); %turns on grid and labels the x-axis
title('EEG Signals - Stacked Plot');
fig0.WindowState = 'maximize'; %maximizes the figure window -> easier to see
%% Tiled Layout - Same Prominence
fig1=figure('Name', 'EEG Signals and Peaks (Prominence=100)');
%Sets the plots to have a tiled layout (7 rows, 2 cols) with tight
% for loop to establish the plots
for i=2:width(T) %1 plot for each sensor
nexttile %moves to next plot
%finds the peaks for each sensor. The peak is currently defined as as
%point that is at least 100 uV above the values directly to the left
%and right of the peak value; this also plots the peaks
findpeaks(T{:,i},T.Timestamp, 'MinPeakProminence', 100)
%creates ylabel as the sensor names, in bold font
ylabel(T.Properties.VariableNames(i), 'FontWeight', 'bold')
xticklabels('') %removes x-tick labels from the plots
if i==14 %for the bottom left plot
xticklabels(xticks) %x-tick labels are present
xlabel('Timestamp (s)'); %xlabel is present
end %ends if loop
end %ends for loop
xticklabels(xticks) %adds x-tick labels to bottom right graph
grid on;
xlabel('Timestamp (s)');
fig1.WindowState = 'maximize';
%% Reg Plot - all EEG Signals and peaks on same plot
%Plot EEG signals on same plot and find their peaks (when the peak is at
%least 100 above the nearest values
fig2=figure('Name', 'EEG Signals and Peaks - Same Plot');
%sets color order to 14 different colors
newcolors={'#0072BD', '#D95319', '#EDB120', '#7E2F8E','#77AC30','#4DBEEE', '#A2142F', '#FF0000', '#00FF00', '#0000FF', '#00FFFF', '#FF00FF', '#FFFF00', '#000000'};
colororder(newcolors); %sets the new color order to use
%Next 15 lines plot findpeaks for all sensors on the same plot - can
%adjust each prominence as desired
findpeaks(T.AF3, T.Timestamp,'MinPeakProminence',100);
grid on; hold on; %turns on grid and plots all peaks on same plot
findpeaks(T.F7, T.Timestamp,'MinPeakProminence',100);
findpeaks(T.F3, T.Timestamp,'MinPeakProminence',100);
findpeaks(T.FC5, T.Timestamp,'MinPeakProminence',100);
findpeaks(T.T7, T.Timestamp,'MinPeakProminence',100);
findpeaks(T.P7, T.Timestamp,'MinPeakProminence',100);
findpeaks(T.O1, T.Timestamp,'MinPeakProminence',100);
findpeaks(T.O2, T.Timestamp,'MinPeakProminence',100);
findpeaks(T.P8, T.Timestamp,'MinPeakProminence',100);
findpeaks(T.T8, T.Timestamp,'MinPeakProminence',100);
findpeaks(T.FC6, T.Timestamp,'MinPeakProminence',100);
findpeaks(T.F4, T.Timestamp,'MinPeakProminence',100);
findpeaks(T.F8, T.Timestamp,'MinPeakProminence',100);
findpeaks(T.AF4, T.Timestamp,'MinPeakProminence',100);
%Creates legend for the plot
legend({'AF3', 'AF3 Peaks', 'F7', 'F7 Peaks', 'F3', 'F3 Peaks', 'FC5', 'FC5 Peaks', 'T7', 'T7 Peaks', 'P7', 'P7 Peaks', 'O1', 'O1 Peaks', 'O2', 'O2 Peaks', 'P8', 'P8 Peaks', 'T8', 'T8 Peaks', 'FC6', 'FC6 Peaks', 'F4', 'F4 Peaks', 'F8', 'F8 Peaks', 'AF4', 'AF4 Peaks'}, 'NumColumns', 2);
title('All Peaks on Same Graph');
xlabel('Timestamp (s)');
ylabel('EEG Value (uV)');
fig2.WindowState = 'maximize';

Accepted Answer

Cris LaPierre
Cris LaPierre on 27 Oct 2021
Edited: Cris LaPierre on 27 Oct 2021
I guess it depends on what you want your markers to be, and how you want to select them. You can manually annotate your plot using the Figure Palette
Christina Diersing
Christina Diersing on 3 Nov 2021
Thank you!
For most of my plots, I used this line of code:
xline(EvtMar,'--',{'EO','BE','BE','BE','EC','BE','CE','CE','BE','OM','OM'}); %Vertical Lines as event markers
For the stacked plots, I used these
ax = findobj(sp.NodeChildren, 'Type','Axes');
grid on; xlabel('Timestamp (s)'); %turns on grid and labels the x-axis
title('EEG Signals - Stacked Plot');
fig0.WindowState = 'maximize'; %maximizes the figure window -> easier to see

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!