Index exceeds the number of array elements (30093)

1 view (last 30 days)
Error in line 36
field_data_stim=field_data(stim_start(i):stim_start(i)+499);
Please check the script and files attached
script:
[unit_dataname,unit_listname]=uigetfile('*txt','Choose a file containing unit data.')
unit_data=textread([unit_listname,unit_dataname],'','emptyvalue',NaN);
[data_length,no_units]=size(unit_data);
stim_data=unit_data(:,end);
unit_data=unit_data(:,1:end-1);
no_units=no_units-1;
[field_dataname,field_listname]=uigetfile('*txt','Choose a file containing field data.')
field_data=textread([field_listname,field_dataname],'','emptyvalue',NaN);
stim_start=find(stim_data==1);
no_stims=length(stim_start);
field_phase_stim=nan(500,20,no_stims);
unit_data_stim=nan(500,no_units,no_stims);
no_stim_spikes=zeros(1,no_units);
center_freqs=linspace(10,80,20);
AVP_stim=zeros(nobins,no_units,20);
MI_stim=zeros(no_units,20);
for i=1:no_stims
field_data_stim=field_data(stim_start(i):stim_start(i)+499);
% Filtering field data.
P=filter_fft_filtfilt_no_save(field_data_stim);
field_phase_stim(:,:,i)=P;
U=unit_data(stim_start(i):stim_start(i)+499,:);
unit_data_stim(:,:,i)=U;
[bincenters,M,L]=amp_v_phase_stenhn(nobins,U,P);
AVP_stim=AVP_stim+M;
no_stim_spikes=no_stim_spikes+sum(unit_data_stim(:,:,i));
end
MI_stim=inv_entropy_save(AVP_stim);
AVP_shuf=zeros(nobins,no_units,20,100);
MI_shuf=zeros(no_units,20,100);
no_shuf_spikes=zeros(100,no_units);
for i=1:100
EEG_shuffle_indices=ceil(rand(1,no_stims)*no_stims);
unit_shuffle_indices=ceil(rand(1,no_stims)*no_stims);
for j=1:no_stims
P_shuf=field_phase_stim(:,:,EEG_shuffle_indices(j));
U_shuf=unit_data_stim(:,:,unit_shuffle_indices(j));
[bincenters,M,L]=amp_v_phase_stenhn(nobins,U_shuf,P_shuf);
AVP_shuf(:,:,:,i)=AVP_shuf(:,:,:,i)+M;
no_shuf_spikes(i,:)=no_shuf_spikes(i,:)+sum(U_shuf);
end
MI_shuf(:,:,i)=inv_entropy_save(AVP_shuf(:,:,:,i));
for k=1:no_units
AVP_shuf(:,k,:,i)=AVP_shuf(:,k,:,i)/no_shuf_spikes(i,k);
end
end
for j=1:no_units
for k=1:20
for i=1:nobins
below_AVP_indices=AVP_shuf(i,j,k,:)<=AVP_stim(i,j,k);
AVP_p_vals(i,j,k)=sum(below_AVP_indices,4)/100;
end
below_MI_indices=MI_shuf(j,k,:)<=MI_stim(j,k);
MI_p_vals(j,k)=sum(below_MI_indices,3)/100;
end
end
for j=1:no_units
figure()
AVP_stim(:,j,:)=AVP_stim(:,j,:)/no_stim_spikes(j);
Prop_shuf(:,j,:)=nanmean(AVP_shuf(:,j,:,:),4);
max_prop=max(max(max(AVP_stim(:,j,:))),max(max(Prop_shuf(:,j,:))));
subplot(3,1,1)
colorplot(AVP_stim(:,j,:))
axis xy
caxis([0 max_prop])
colorbar
set(gca,'XTick',[1:nobins]+.5,'YTick',[1:no_freqs]+.5,'XTickLabel',bincenters,'YTickLabel',center_freqs)
title('Percent Spikes by Phase of LFP Oscillations (Auditory Stimulus)')
xlabel('Oscillation Phase (Radians)')
ylabel('Oscillation Frequency (Hz)')
subplot(3,1,2)
colorplot(Prop_shuf(:,j,:))
axis xy
caxis([0 max_prop])
colorbar
set(gca,'XTick',[1:nobins]+.5,'YTick',[1:no_freqs]+.5,'XTickLabel',bincenters,'YTickLabel',center_freqs)
title('Percent Spikes by Phase of LFP Oscillations (Mean, 100 Random Shuffles)')
xlabel('Oscillation Phase (Radians)')
ylabel('Oscillation Frequency (Hz)')
subplot(3,1,3)
colorplot(AVP_p_vals(:,j,:))
caxis([0 1])
colorbar
axis xy
set(gca,'XTick',[1:nobins]+.5,'YTick',[1:no_freqs]+.5,'XTickLabel',bincenters,'YTickLabel',center_freqs)
title('Empirical p-Value of Spikes by Phase (Auditory Stimulus >= Percent of Random Shuffles)')
xlabel('Oscillation Phase (Radians)')
ylabel('Oscillation Frequency (Hz)')
saveas(gcf,['Unit_',num2str(j),'_svp'],'pdf')
for k=1:20
figure()
plot(bincenters,reshape(AVP_shuf(:,j,k,:),nobins,100),'y')
hold on
plot(bincenters,AVP_stim(:,j,k),'k')
title({['Proportion of Unit ',num2str(j),' Spikes by Phase of ',num2str(center_freqs(k)),' Hz Oscillation'];'Actual LFP (Black) vs. 100 Shuffled LFP Segments (Yellow)'})
saveas(gcf,['Unit_',num2str(j),'_by_',num2str(round(center_freqs(k))),'_Hz_svp'],'pdf')
close(gcf)
end
end
figure()
MI_shuf_mean=mean(MI_shuf,3);
max_MI=max(max(max(MI_stim)),max(max(MI_shuf_mean)));
subplot(1,3,1)
colorplot(MI_stim')
axis xy
caxis([0 max_MI])
colorbar
set(gca,'XTick',[1:no_units]+.5,'YTick',[1:no_freqs]+.5,'XTickLabel',[1:no_units],'YTickLabel',center_freqs)
title({'Modulation of Spikes by LFP Oscillations';'(Auditory Stimulus)'})
xlabel('Unit')
ylabel('Oscillation Frequency (Hz)')
subplot(1,3,2)
colorplot(MI_shuf_mean')
axis xy
caxis([0 max_MI])
colorbar
set(gca,'XTick',[1:no_units]+.5,'YTick',[1:no_freqs]+.5,'XTickLabel',[1:no_units],'YTickLabel',center_freqs)
title({'Modulation Spikes by Phase of LFP Oscillations';'(Mean, 100 Random Shuffles)'})
xlabel('Unit')
ylabel('Oscillation Frequency (Hz)')
subplot(1,3,3)
colorplot(MI_p_vals')
caxis([0 1])
colorbar
axis xy
set(gca,'XTick',[1:no_units]+.5,'YTick',[1:no_freqs]+.5,'XTickLabel',[1:no_units],'YTickLabel',center_freqs)
title({'Empirical p-Value of Modulation of Spikes by Phase';'(Percent of Random Shuffles <= Auditory Stimulus)'})
xlabel('Unit')
ylabel('Oscillation Frequency (Hz)')
saveas(gcf,['Unit_',num2str(j),'_MI'],'pdf')

Accepted Answer

Prudhvi Peddagoni
Prudhvi Peddagoni on 18 Jan 2021
Hi,
In the 19th line that contains the below line in your code,
field_data_stim = field_data(stim_start(i):stim_start(i)+499);
the field_data variable length is 30093. Where as some of the elements in stim_start variable are greater than (30093 - 499). So when you are trying to access field_data(stim_start(i)+499), the index is greater than 30093. So you can add an if statement to check if stim_start(i) +499 is greater than length(field_data) and change the code like this:
if stim_start(i)+499 > length(field_data)
field_data_stim = field_data(stim_start(i):length(field_data));
else
field_data_stim= field_data(stim_start(i):stim_start(i)+499);
end
Hope this helps.
  2 Comments
Steve
Steve on 18 Jan 2021
Thank you, really appreciate it.
But have this issue at line 28 or 29 below "P=filter_fft_filtfilt_no_save(field_data_stim);"
Error;
"Unable to perform assignment because the size of the left side is 500-by-20 and the size of the right side is 477-by-20."
Error in Stenhn_analysis_try_V1b (line 29)
field_phase_stim(:,:,i)=P;
Steve
Steve on 30 Jan 2021
Error;
"Unable to perform assignment because the size of the left side is 500-by-20 and the size of the right side is 477-by-20."
Error in Stenhn_analysis_try_V1b (line 29)
field_phase_stim(:,:,i)=P;

Sign in to comment.

More Answers (0)

Categories

Find more on Electrophysiology in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!