Index exceeds the number of array elements in for loop

조회 수: 1 (최근 30일)
Hussein Kokash
Hussein Kokash 2021년 6월 20일
편집: Stephen23 2021년 6월 22일
Dears, hope you are doing fantastic.
I have a script that I use to find wavelength and wavenumber by uploading text files and define the values for x an y.
It goes ok for one file, however; with mutliple files I have an "Index exceeds the number of array elements" error
A help will be grestly appreciated, thanks :)
The script goes this way:
[file_list, path_n] = uigetfile('.txt', 'Multiselect', 'on');
filesSorted = natsortfiles(file_list);
if iscell(filesSorted) == 0;
filesSorted = (filesSorted);
end
for i = 1:length(filesSorted);
filename = filesSorted{i};
data = load([path_n filename]);
% Define x and y from the uploaded files
x = data (:,1);
y = data (:,2);
% Calculate Wavelength and Wavenumber K from the uploaded files
[maxvaly,idx] = max(y);
maxvalx = x(idx);
[idx,idx]=findpeaks(y);
Wavelength(i)=x(idx(2))-x(idx(1))
Wavenumber(i)=(2*pi)/Wavelength(i)
end

채택된 답변

Walter Roberson
Walter Roberson 2021년 6월 20일
Wavelength(i)=x(idx(2))-x(idx(1))
Your code is assuming that findpeaks() will always be able to find at least two peaks. You should not assume that. You should be checking length() of the result.
  댓글 수: 5
Hussein Kokash
Hussein Kokash 2021년 6월 21일
Never mind, you were absolutely right, the shape of the wave was irregular and that is why I had a NaN output, it has to be fitted into a function first and then get the wavelength properly.
Thank you and god bless!
Walter Roberson
Walter Roberson 2021년 6월 21일
sorry, I do not know much about the Algorithm. You could experiment with findpeaks of (say) 1000 times the input signal.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 6월 20일
The problem is with this 3rd party fcn file: filesSorted = natsortfiles(file_list);
Note that the variable file_list contains th being imported file name, e.g.: 'ABC123.txt'
So, the err is occuring on: filesSorted = natsortfiles(file_list);
and then filename = filesSorted{i}; is not giving the following file name as you've anticipated.
  댓글 수: 2
Hussein Kokash
Hussein Kokash 2021년 6월 20일
Thanks Sulaymon, What can be done here to solve it and keep the pattern of importing files intact?
Stephen23
Stephen23 2021년 6월 22일
편집: Stephen23 2021년 6월 22일
"What can be done here to solve it and keep the pattern of importing files intact?"
Nothing is required to "solve" it, because NATSORTFILES is certainly not the cause of that error.
Lets consider the possible configurations of UIGETFILE's first output:
  • user selects multiple files: NATSORTFILES sorts and and returns a cell array of char vectors,
  • user selects one file: NATSORTFILES returns the one char vector,
  • user selects zero files: NATSORTFILES throws an error due to the invalid input data class (but this error is moot, because your code cannot anyway handle this case: you can decide if you want to handle this case yourself and add the appropriate code).
However there is an obvious bug in your code, because you used parentheses instead of curly braces here:
filesSorted = (filesSorted);
% ^ ^ these do nothing at all
It should be:
if ~iscell(filesSorted)
filesSorted = {filesSorted};
end % ^ ^ you need curly braces!
or simply replace the entire IF-statement with this:
filesSorted = cellstr(filesSorted);
That bug will cause an error to be thrown later in your code (but is totally unrelated to your NATSORTFILES useage, or the error you gave in your question, or anything written in this incorrect answer).

댓글을 달려면 로그인하십시오.


Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 6월 20일
Easy solution is to get all file names to be read (to be imported in *.txt). That can be done with:
Finfo = dir('*.txt');
Fnames = {Finfo.name};
for i = 1:length(Fnames);
filename = Fnames{i};
...
end
  댓글 수: 1
Hussein Kokash
Hussein Kokash 2021년 6월 20일
I replaced what I have with what you suggested, unfortunately still the same error of:
Index exceeds the number of array elements (0).
Error in test (line 40)
Wavelength(i)=x(idx(2))-x(idx(1))

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2021년 6월 20일
[idx,idx]=findpeaks(y);
is not right. It should be
[peakValues, indexesOfPeaks] = findpeaks(y);
  댓글 수: 5
Image Analyst
Image Analyst 2021년 6월 21일
The you can do
% Get the x values of all the peaks
xPeaks = x(indexesOfPeaks);
% Find distance between adjacent peaks.
peakSpacings = diff(xPeaks);
% Find the mean peak-to-peak spacing.
Wavelength = mean(peakSpacings)
Hussein Kokash
Hussein Kokash 2021년 6월 22일
Thank you for your valuable input, appreciate it, will try it for sure.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by