Why does my gaussian fit give an error?

4 views (last 30 days)
JJH
JJH on 5 Dec 2018
Commented: Image Analyst on 5 Dec 2018
I have a code that loops through a set of .csv files and imports the data stored in them as Wavelength and Intensity and correctly returns the wavelength and intensity as arrays. I then want to fit the data to a Gaussian. Some of the files have all of the values of the intensity being zero, so I have put a check in for this. The code currently is
for kk = 2:DataPointsSpec+1 % loop through each file - DataPointsSpec defined previously
SpectrumData = PeakFit([FolderName '\' Direc(kk).name]); % only relevant thing here is csvread for each file
clear Wavelength % this doesn't seem to help
clear Intensity
Wavelength = SpectrumData(2:end-1,1); %ignore first and last point in data (not relevant)
Intensity = SpectrumData(2:end-1,2);
peak = findpeaks(Intensity,'NPeaks',1,'MinPeakProminence',1*10^-3,'MinPeakDistance',20); % determines whether or not a peak is present
if isempty(peak)
peakposarray(kk) = nan % if there is no peak, print NaN in place of the peak wavelength
else
gaussianfit = fit(Wavelength,Intensity,'gauss2') % if there is a peak, fit it to a Gaussian
end
end
However, when I try to do a Gaussian fit on the non-zero data, I get an error saying 'Index in position 1 is invalid. Array indices must be positive integers or logical values.' I'm not sure why there is a problem, as all I want to do is to fit some data that is in the form [x,y].
  1 Comment
Image Analyst
Image Analyst on 5 Dec 2018
Provide the data, full code, and FULL error message (including line of code and line number) so that people can reproduce and fix the problem.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!