This site is a work in progress.
When working with EEG data, it is very useful to know exactly which 'trial' corresponds to each 'epoch' you are looking at - because it allows you to match your EEG data with information stored outside of EEGLAB. For example, I measure the force of responses for each trial in a separate excel spreadsheet. If you know exactly 'which-trial-is-which' you can do all sorts of stuff your data, like sorting trials by this external information and look at other things like sequence effects.
However, keeping track of your trials can be difficult because (1) EEGLAB stores information about the trial (e.g. event-markers) and the actual EEG signal separately, and (2) native information about the trial can be unclear because (a) the trial-types are usually denoted by numbered event markers (not very descriptive, easily forgotten), and (b) the information denoted by event-markers is limited - it may tell you which stimulus was presented and whether a response was made, but the extract trial-number is not clear and calculating response time (RT) from the event-markers takes a few more steps. (3) Common pre-processing procedures such as expoching a particular type of trial (e.g. only extracting 'standard' trials) and deleting trials with artifacts, can cause you to lose track of your data.
In this section, I will show you how I manage my trial-level EEG data.
My approach is to preserve all trials in the EEG and external information stored in my excel spreadsheet (which I call my behavioral data - this contains information such as subject id, trial number, trial type, RT, Force, and anything I think i'll use). This means,If I have 300 trials in my experiment, I make sure to extract 300 epochs and have 300 trials in my excel spreadsheet. During EEG pre-processing, i use the readtable() function to import the excel spreadsheet data for that participant and store it as EEG.etc.behavioural_data. This way I know that each epoch in my EEG data corresponds to a row in my excel spreadsheet.
To extract EEG data from particular trials, I use boolean indexing to identify the row numbers I want use in the behavioural data. Because the EEG data has the same number of trials in the same order, I can use these row numbers to get the EEG data that I want.
For example, let's say I want to extract 'standard' trials. In my excel spreadsheet, I have a column called 'trial_type' which has information about whether a trial is a 'standard' or a 'target'. Once I import this to MATLAB, I can use the command trials2use = strcmpi(EEG.etc.behavioural_data.trial_type,'standard') to ask MATLAB which trials are the 'standard' ones. This will return a list of 1's and 0's for each trial, and the 1's correspond to the 'standard' trials, stored in the trials2use variable. Because the EEG data has the same number of trials, I can apply this to the EEG data (The EEG data is a 3-dimensional matrix of Channels x Time Points x Trials). To get the trials you want, you can use the command EEG.data(:,:,trials2use). This will extract the data of all channels and timepoints, for all 'standard' trials.
If you wanted to plot the averaged ERP waveform of this data, (1) you will need to tell MATLAB exactly which trial you want to plot and (2) you will need to compute an average over trials.
Let's say I want to plot the data at 'Cz'. In my data, 'Cz' corresponds to the 48th element in the 1st dimension of EEG.data (which denotes channels). To extract this individual channel you would go standard_cz_trials = EEG.data(48,:,trials2use), and to average over trials you would go standard_erp = mean(EEG.data(48,:,trials2use),3). The '3' means to average over the 3rd dimension, which is trials. To select a specific channel based on the label (e.g. 'FCz'), I also use boolean indexing on EEG.chanlocs.labels by using channel2use = strcmpi({EEG.chanlocs.labels},{'FCz'}) and you can replace 'FCz' with another label - you just have to make sure that it matches the information in the EEG.chanlocs.labels structure. Then you could go standard_erp = mean(EEG.data(channel2use,:,trials2use),3). This will give you a single list of values indicating the average voltage at each time-point at Cz for standard trials, and you can plot this using plot(EEG.times,standard_erp). For more information on plotting waveforms, have a look at the content page on plotting.
If you wanted to plot the averaged scalp distribution, you will need to tell MATLAB (1) what trials you want and (2) what time-point/range you want to plot over. A scalp map shows the mean voltage over a period of time by using colours (e.g. blue for negative and red for positive). To specify trials, you can use the same method as above. To specify a time range, let's say I want the mean scalp distribution over the first 20 samples on standard trials, I would go like this scalp2plot = mean(EEG.data(:,1:20,trials2use),[2, 3]). So the second dimension in EEG.data denotes time-points, and the [2, 3] in the mean section denotes that I want the average voltage across time, as well as trials. Then you would plot that by using the topoplot() function, so you would go topoplot(scalp2plot,EEG.chanlocs,'electrodes', 'on','shading','flat'). For more information on plotting scalp maps, have a look at the content page on plotting.
If you want to measure the mean amplitude over a time-range at a specific trial, you need to tell MATLAB (1) what channel you want and (2) what time-range you want. To keep the trial numbers the same, I just measure on all trials and 'clear' the voltages of trials I don't want. This assumes that you want to measure the same time-range for trials/trial-types (which is usually the case if you want to compare across conditions).