I am trying to extract the alpha power from a number of parietal electrodes. Here attached the code:
baseDir = 'my local folder'; %directory where everything is based
resultsFileName = [baseDir 'trialResultsAllSubjects.xlsx'];
allResultsTable = readmatrix(resultsFileName,'Basic',false); %load the matrix of trial timings
eeglabDir = 'C:\Users\hype2\Documents\MATLAB\eeglab2022.0'; %eeglab directory as we need some functions
addpath(genpath(eeglabDir)); %add folders and subfolders to path
channelsToPlot = [21 22 25 26 27];
channelsLabel = {'cp1', 'cp2', 'p3', 'pz', 'p4'}; % these are the electrodes names
doFig = 0; %if set to 1 then a time frequency figure for every trial will be plotted, this is 4s before and after the midpoint
for thisSubjectNum = 8 %the number of subject whose EEG is currently loaded
subjectString = sprintf('%02d',thisSubjectNum);
eegFileName = [baseDir subjectString '\pilot_' subjectString '_filt_postICA_lowfilt_reref.set'];
[ALLEEG, EEG, CURRENTSET, ALLCOM] = eeglab;
EEG = pop_loadset('filename', eegFileName);
[ALLEEG, EEG, CURRENTSET] = eeg_store(ALLEEG, EEG, 0);
trialList = find(allResultsTable(:,1)==thisSubjectNum);
for trialNum = trialList'
timeMid = allResultsTable(trialNum,13);
if doFig
figure;
out = pop_newtimef( EEG, 1, 26, [timeMid*1000-4000 timeMid*1000+4000], 0);
end
out = pop_spectopo( EEG, 1, [timeMid*1000-1000 timeMid*1000+1000],'EEG','freqrange',[2 35],'freqs',5);
%get the average pwoer in the 8:12 Hz frequency band for the channels
%of interest
bandMus = mean(out(channelsToPlot,8:12),2);
%copy to correct columns of results table
allResultsTable(trialNum,7:11) = bandMus;
end
end
%now save
writematrix(allResultsTable,'.\trialResultsAllSubjectsComplete.xlsx')
So I have 10 subjects and i have repeated the process for subjects 1,2,3,4,5,6,7 without any issues. However for subjects 8,9,10 i get an error that says:
Error using spectopo
Spectopo: non-integer number of epochs
Error in pop_spectopo (line 331)
eval(com)
Error in getTfrFromMidPoint (line 32)
out = pop_spectopo( EEG, 1, [timeMid*1000-1000 timeMid*1000+1000],'EEG','freqrange',[2 35],'freqs',5)
I havent written the code - my supervisor has, and I am really at a loss as to how is it possible that it works perfectly by changing the ThisSubjectNum to 1 through 7 but not 8,9,10. Thanks in advance!
I tried to review the spectopo and pop_spectopo functions but they are too complex and i dont understand. However i found in the function script that the non-integer error is generated when this happens:
epochs = round(size(data,2)/frames);
if frames*epochs ~= size(data,2)
error('Spectopo: non-integer number of epochs');
end
but i am not quite sure what frames are in terms of eeg datasets.
also, this script relies on another function which i attach below: %script to get relative time for each trial and start building results %table.
%in the table (when loaded as a matrix):
%for each trial to understand which time point to extract from the EEG we
%need to find the mid point of the ball movement to the corner.
%this is
% column 3 is overall trial number (including practice trials and isntruction screens)
% 4 is block number:
% (1= practice1 instruction, 2= practice 1, 3= practice 2 instruction,...
% 4 = practice 2, 5 = block 1 instruction, 6= block 1, 7 = block 2
% instruction, 8 = block 2, 9 = block 3 instruction, 10 = block 3)
%ballstart time is column 9
%estimated mid time (light off) is 10
%actual mid time is 11
%estimated end time is 12
%actual end time is 13 (but need to subtract 5s from this)
%15 is which corner the ball goes to (1=BL,2=BR,3=FL,4=FR)
%16 is movement speed of the ball.
eegVRTimeLags = [213.6 545.64 929.2 nan 469.87 200.84 320 710.8 573.6 774.5]; %when VR was time 0, EEG was this time in seconds
%in results table columns will be
%1 = subjectnum
%2 = trialnumber (only real trials so 1:96)
%3 = end point of the ball (1=BL,2=BR,3=FL,4=FR)
%4 = speed of the ball (0.5, 1, 1.5 or 2)
%5 = the estiamted time length of ball travel
%6 = the actual time length of ball travel
%7 = alpha power at 'cp1'
%8 = 'cp2'
%9 = 'p3'
%10 = 'pz'
%11 = 'p4'
%12 = time point in eeg to extract in VR headset time
%13 = time point in EEG time to extract in EEG time
%TODO need to think about how to save synchronsiation information
trialsToAnalyse = [12:43 45:76 78:109]; %this refers to row numbers in the data matrix loaded here
numSubjects = 10;
allResultsTable = nan(numSubjects*96,11);
trialCounter = 1;
for subjectNum = 1:numSubjects
thisSubFolder = ['C:\Users\ASUS\OneDrive - Goldsmiths College\Attachments\DISSERTATION\' sprintf('%02d',subjectNum),'\'];
trialData = readmatrix([thisSubFolder 'trial_results.csv']);
tempTrialCounter = 1;
for trialNum = trialsToAnalyse
allResultsTable(trialCounter,1) = subjectNum;
allResultsTable(trialCounter,2) = trialCounter;
allResultsTable(trialCounter,3) = trialData(trialNum,15);
allResultsTable(trialCounter,4) = trialData(trialNum,16);
allResultsTable(trialCounter,5) = trialData(trialNum,12)-trialData(trialNum,10);
allResultsTable(trialCounter,6) = trialData(trialNum,13)-trialData(trialNum,11)-5;
allResultsTable(trialCounter,12) = (trialData(trialNum,13)+trialData(trialNum,11)-5)/2;
allResultsTable(trialCounter,13) = allResultsTable(trialCounter,12)+eegVRTimeLags(subjectNum);
tempTrialCounter = tempTrialCounter +1;
trialCounter = trialCounter+1;
end
end
writematrix(allResultsTable,'.\trialResultsAllSubjects.xlsx')