Index exceeds the number of array elements. Index must not exceed 0.

123 views (last 30 days)
Hello everyone, i'm running into a problem when I try to do my for loop, i'm trying to open and use the files 1-11. But as I try to run the code using the for loop, my peaks and locs arrays suddenly become empty. I think I am loading the data in incorrectly, but can't really seem to find anything about the matter.
The error: Index exceeds the number of array elements. Index must not exceed 0.
Error in Radialvelocity (line 98)
if pks(1) > pks(2)
%Data
files = dir("C:\Users\Jeffs\OneDrive\Documents\MATLAB\KIC4054905");
files([files.isdir]) = []; %remove folders including . and ..
data_reference = load('C:\Users\Jeffs\OneDrive\Documents\MATLAB\5750_45_p00p00_template.dat');
%Test
Test=load(files(1).name);
%Test
Test=load(files(1).name);
TestWave=Test(:,1); TestSolarFlux=Test(:,2);%Trial waves
%Plotting
fig1=figure(1);
hold on;
plot(TestWave,TestSolarFlux,'b')
plot(data_reference(:,1),data_reference(:,2),'r')
title('Original data')
xlabel('Wavelength[Aangstrom]')
ylabel('Solar flux')
hold off;
%Start = 5000.35Å; end = 5497.52Å
LambdaInterpolate=lambdavector(5e03, 5.497e3,1)
%New tests
New_test_flux = interp1(Test(:,1),Test(:,2),LambdaInterpolate);
New_comparison = interp1(data_reference(:,1),data_reference(:,2),LambdaInterpolate);
%Plotting
fig2=figure(2);
hold on;
title('Interpoleret Data')
plot(LambdaInterpolate,New_test_flux,'b')
plot(LambdaInterpolate,New_comparison,'r')
xlabel('Wavelength[Aangstrom]')
ylabel('Solar flux')
hold off;
%Crossrelations
[crossrelationTest,lagTest]=xcorr(New_test_flux,New_comparison,200)
%Plotting
fig3 = figure(3);
hold on
title('Crosskorrelation as func of lag/radialvelocity)')
xlabel('Lag/radialvelocity[km/s]')
ylabel('Crosskorrelation')
grid on;
plot(lagTest,crossrelationTest,'r')
%Peaks
[pks locs] = findpeaks(crossrelationTest,lagTest,'MinPeakProminence',4,'Annotate','extents');%Det lader til at virke ved 4.
% plot(-108,1.3497*1.0e+04,'.b','markersize',20)
% plot(1,1.3485*1.0e+04,'.b','markersize',20)
%Constant:
LambdaInterpolate=lambdavector(5334,5.6124e+03,1);
New_comparison=interp1(data_reference(:,1),data_reference(:,2),LambdaInterpolate);
%We need arrays for data
v1=[];
v2=[];
Times=[];
for i=1:11
DataAll=load(files(i).name);
Time=files(i).datenum;
Times=[Times Time];
LambdaAll=DataAll(:,1);
FluxerAll=DataAll(:,2);
New_flux=interp1(LambdaAll,FluxerAll,LambdaInterpolate);
%Crossrelations
[crossrelationDataAll,lagDataAll]=xcorr(New_flux,New_comparison,200,'coef');
%Determining radialvelocities
[pks locs widths proms] = findpeaks(crossrelationDataAll,lagDataAll,'MinPeakProminence',0.004,'Annotate','extents');
fig2=figure(2);
hold on;
plot(lagDataAll,crossrelationDataAll,'r')
if length(pks) == 1 %Her overlapper hastighedderne, altså de er ens/krydser.
v1=[v1 locs(1)];
v2=[v2 locs(1)];
else
if pks(1) > pks(2)
v1=[v1 locs(1)];
v2=[v2 locs(2)];
else
v1=[v1 locs(2)];
v2=[v2 locs(1)];
end
end
end
%Function
function vector = lambdavector(lstart,lslut,dv)
c=2.9979e5;%Enhed km/s.
n=log10(lslut/lstart)/(log10(1+dv/c))+1;
vector=[];
for i=1:n
li=lstart*(1+dv/c).^(i-1);
vector=[vector,li];
end
vector;
end

Answers (2)

Voss
Voss on 14 Dec 2021
This happens because pks is empty, i.e., findpeaks returned no peaks. You will need to modify your code to handle this situation.
  2 Comments
Jeff Sejr
Jeff Sejr on 14 Dec 2021
So it's because of how I load the files? I mean there's obviously peaks, and before I made the for loop I had 19 peaks and 19 locs
Voss
Voss on 14 Dec 2021
I don't think it's necessarily caused by how the files are loaded.
Maybe step through your code as it runs and make sure the values of the variables are as expected as it goes. If you find something weird, you'll have to figure out why that is, perhaps by comparing what is happening now vs what happened under the old version without the for loop.

Sign in to comment.


Image Analyst
Image Analyst on 14 Dec 2021
Try adding this after the call to findpeaks():
[pks locs widths proms] = findpeaks(crossrelationDataAll,lagDataAll,'MinPeakProminence',0.004,'Annotate','extents');
if isempty(pks)
warningMessage = sprintf('Warning: there were no peaks found.\nTry adjusting your parameters')
uiwait(warndlg(warningMessage));
continue;
end
  3 Comments
Image Analyst
Image Analyst on 14 Dec 2021
OK, so there are no peaks found with default settings. If you want to find all peaks, try islocalmax().
If you need any more help, please re-read the posting guidelines and do what it says:
Jeff Sejr
Jeff Sejr on 14 Dec 2021
Thanks for trying to help, if I do tt = islocalmax(crossrelationDataAll), I get no peaks.
However, I have found that in my New_flux and New_comparison, I have multiple values of NaN

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!