Clear Filters
Clear Filters

Index in position 1 exceeds array bounds. Index must not exceed 18.

33 views (last 30 days)
To whom it may concern,
I keep getting the following errors after running a code for EEG resting analysis. It is produced after finishing ICA and removing components.
  1. The variable a ( cointaining the ICA process doesn't save in the workspace). I have already tried others tips such as checking eeg_options and adding it to path 'userpath', but still it doesn't work.
  2. The error matlab announces is:
Index in position 1 exceeds array bounds. Index must not exceed 18.
Error in eeg_getdatact (line 196)
data = (EEG.icaweights(opt.component,:)*EEG.icasphere)*data(EEG.icachansind,:);
Error in pop_eegthresh (line 200)
icaacttmp = eeg_getdatact(EEG, 'component', elecrange);
I have been reading the other responses to this bug posted here but I don't seem to be able to find the mistake that must be present in the code.
The code is the following ( the long one): And the problems start in the "Acotar amplitudes" section ( that I repeat next)
a=size(EEG.icaact,1);
pop_eegthresh(EEG,0,1:a,-100,100,-0.2,0.2, 1, 1);
pause(10);
clear
clc
eeglab
%% A configurar según sujeto
ruta='C:\Users\rebec\OneDrive\Escritorio\DATOS-EEG\PACIENTES\';
sujeto="PAC01";
set_sufix=".set";
%% Cargar archivo vhdr
EEG.etc.eeglabvers = '2023.0'; % this tracks which version of EEGLAB is being used, you may ignore it
EEG = pop_loadbv(ruta, sujeto+".vhdr", [1 300000]); %incluyo el rango de Interval de [1 300000] para incluir los 300ms iniciales con los ojos cerrados
EEG=pop_chanedit(EEG, []); %%se debe seleccionar el fichero MNI coordinate file for BEM dipfit model. En concreto, el plugins\dipfit\standard_BEM\elec\standard_1005.elc
ORG=EEG;% Guardo copia para poder obtener los índices de los canales originales (independientemente de si se han eliminado o no)
%% Eliminar canales que a priori sabemos que son ruidosos porque en el registro no funcionaban identificados mediante inspección visual MR
badchans=[42,63]; % Canales para cada sujeto: C03: [27]; C04: [40]; C05:[22 34 40]; C06, 7, 8:[40]; PAC01: [VEOGn,42,63], PAC02: [32]; PAC03 [11,15,27,33,37,47,51,62] PAC04: [42]; PAC05 [57],PAC06: [57]; PAC07: ningún electrodo ; PAC08 [42]; PAC09: [40]; PAC10: [40];
chansInterpolar=zeros(1, length(badchans));
i=0
for n=1:length(badchans)
if (badchans(n)>=47 && badchans(n)<=53) || (badchans(n)>=55 && badchans(n)<=61) || (badchans(n)>=21 && badchans(n)<=27) % Si el canal eliminado es uno de los electrodos de la linea central, lo interpolamos
chansInterpolar(n) = badchans(n);
i=i+1;
end
end
chansInterpolar=trimdata(chansInterpolar, i);
EEG = pop_select(EEG, 'rmchannel', badchans); % para c05 pero cambiar para cada registro los valores dentro de [] tras inspeccion visual
EEG=pop_chanedit(EEG, []);
%% Interpolar los canales eliminados MR
if length(chansInterpolar)>0
EEG = eeg_interp(EEG, ORG.chanlocs(chansInterpolar), 'spherical', [EEG.xmin EEG.xmax]); %%el canal interpolado se introduce en la posicion 63 así que hay que verificar en cada registro EEG.chanlocs para calcular a posteriori los parámetros qEEG
EEG=pop_chanedit(EEG, []); %%lo añado solo para comprobar que se han interpolado los canales que se han indicado
eegplot();
end
%% Aplicar filtro notch a 50Hz
EEG = pop_eegfiltnew(EEG, 'locutoff',49,'hicutoff',51,'revfilt',1,'plotfreqz',1);
EEG=pop_chanedit(EEG, []); %quitar localizaciones A1
%% Eliminar canales cuyo espectro se desvíe de un umbral, por defecto 5
try
chHEOGn=find(strcmp({EEG.chanlocs.labels}, 'HEOGn'));
chVEOGp=find(strcmp({EEG.chanlocs.labels}, 'VEOGp'));
end
EEG=pop_rejchanspec(EEG, 'plotchans', 'on','stdthresh',3, 'elec', [1:(chHEOGn-1) (chVEOGp+1):EEG.nbchan]); % los valores de los indices de los canales varían en función de si se han eliminado canales
pause(25)
eegplot();
%% Aplicar análisis ICA a todos los canales excepto VEOG y HEOG
EEG = pop_runica(EEG, 'icatype', 'runica', 'extended',1,'interupt','on','chanind',[1:(chHEOGn-1) (chVEOGp+1):EEG.nbchan]);%los valores de los indices tras chanind se cambian si se han eliminado canales, OJO en PAC02 que se elimina VEOGp
% Guardar datos en formato .set y .fdt
nombre=sujeto+"_5min.previos_ICA_refA2";
EEG.setname = nombre;
filename=char(nombre+set_sufix);
EEG = pop_saveset(EEG, 'filename',filename , 'filepath', ruta);
EEG = pop_iclabel(EEG, 'default');
% Marcar como artefactos aquellos componentes que se deban en menos de un 10% al cerebro:
% thresh - array with threshold values with limits to include for selection as artifacts.
% The 6 categories are (in order) Brain, Muscle, Eye, Heart, Line Noise, Channel Noise, Other.
EEG = pop_icflag(EEG, [0 0.1;NaN NaN;NaN NaN;NaN NaN;NaN NaN;NaN NaN;NaN NaN]);
EEG = pop_subcomp( EEG, [], 0);
nombre=nombre+"done";
EEG.setname=nombre;
filename=char(nombre+set_sufix);
EEG = pop_saveset(EEG, 'filename', filename , 'filepath', ruta);
%% Acotar amplitudes
a=size(EEG.icaact,1);
pop_eegthresh(EEG,0,1:a,-100,100,-0.2,0.2, 1, 1);
pause(10);
%% Filtrado en banda de interés
m=EEG.chanlocs.labels; %por si se ha eliminado algún canal
EEG = pop_eegfiltnew(EEG, 'locutoff',0.5,'hicutoff',50,'plotfreqz',1,'channels',cellstr(m));
nombre=nombre+"_filt";
EEG.setname=nombre;
filename=char(nombre+set_sufix);
EEG = pop_saveset(EEG, 'filename', filename , 'filepath', ruta);
%% Obtener épocas del EEG continuo
EEG=eeg_regepochs(EEG, 'recurrence', 2, 'limits', [0 2]); %al ser espectro continuo, hay que usar esta función que genera eventos aleatorios para poder extraer las épocas
nombre=nombre+"_epochs";
EEG.setname=nombre;
filename=char(nombre+set_sufix);
EEG = pop_saveset(EEG, 'filename', filename , 'filepath', ruta);
%% Guardar imágenes
rutaGeneral='C:\Users\rebec\OneDrive\Escritorio\DATOS-EEG\PACIENTES\';
rutaEspecifica=sujeto;
rutaArchivo=rutaGeneral+rutaEspecifica;
%% Estudiar espectro de potencia del paciente en 5 mins previos
rango=[1 30];
intervalo_temporal=[0 300];
frecuencias_delta=[1 : 4];
frecuencias_theta=[4:8];
frecuencias_alpha=[8:12];
frecuencias_beta1=[12:19];
frecuencias_beta2=[20:25];
frecuencias_beta3=[26:30];
delta=figure; pop_spectopo(EEG, 1, intervalo_temporal, EEG , 'freq', frecuencias_delta, 'freqrange',rango,'electrodes','off');
nombreArchivo="Espectro potencia delta 5mins relax";
saveas(delta, rutaArchivo+nombreArchivo, 'jpeg');
theta=figure; pop_spectopo(EEG, 1, intervalo_temporal, 'EEG' , 'freq', frecuencias_theta, 'freqrange',rango,'electrodes','off');
nombreArchivo="Espectro potencia theta 5 mins relax";
saveas(theta, rutaArchivo+nombreArchivo, 'jpeg');
alpha=figure; pop_spectopo(EEG, 1, intervalo_temporal, 'EEG' , 'freq', frecuencias_alpha, 'freqrange',rango,'electrodes','off');
nombreArchivo="Espectro potencia alpha 5 mins relax";
saveas(alpha, rutaArchivo+nombreArchivo, 'jpeg');
beta1=figure; pop_spectopo(EEG, 1, intervalo_temporal, 'EEG' , 'freq', frecuencias_beta1, 'freqrange',rango,'electrodes','off');
nombreArchivo="Espectro potencia beta1 5 mins relax";
saveas(beta1, rutaArchivo+nombreArchivo, 'jpeg');
beta2=figure; pop_spectopo(EEG, 1, intervalo_temporal, 'EEG' , 'freq', frecuencias_beta2, 'freqrange',rango,'electrodes','off');
nombreArchivo="Espectro potencia beta2 5 mins relax";
saveas(beta2, rutaArchivo+nombreArchivo, 'jpeg');
beta3=figure; pop_spectopo(EEG, 1, intervalo_temporal, 'EEG' , 'freq', frecuencias_beta3, 'freqrange',rango,'electrodes','off');
nombreArchivo="Espectro potencia beta3 5 mins relax";
saveas(beta3, rutaArchivo+nombreArchivo, 'jpeg');
% Análisis de potencia espectral
name=paciente+'eegstats';
pop_eegstats(EEG, 'thetarange',[4 8],'alpharange',[8 12], 'otherranges', [1 4; 12 30; 30 50], 'averagepower', 'off', 'channels', [(11):(63)],'csvfile', 'C07_eegstats', 'iaf', 'off' );
The image above is from the matlab workspace in case it may be of help.
Thanking in advance to whoever may try to help me,

Accepted Answer

Torsten
Torsten on 24 Jul 2024 at 20:32
Moved: Torsten on 24 Jul 2024 at 20:41
data = (EEG.icaweights(opt.component,:)*EEG.icasphere)*data(EEG.icachansind,:);
You are aware that you overwrite the matrix "data" here, aren't you ?
  1 Comment
Rebeca
Rebeca on 24 Jul 2024 at 20:50
Hi, thanks for answering. I hadn't realised. I you find any other mistakes please let me know. Thank you for your time!

Sign in to comment.

More Answers (0)

Categories

Find more on EEG/MEG/ECoG in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!