Fitting a Gaussian to multiple data sets
2 views (last 30 days)
Show older comments
I am trying to analyse a spectral feature over time, attached is an image of the feature and the colour grading is with respect to time, black is T=0, red is T=end. I have tried to find a solution to this from other answered questions but am drawing a blank. What I'm trying to do is loop through each data set and fit a gaussian model to each, I can achieve this with the following code:
clear all
%Importing file
sixthpks = csvread('Peaks_1570-1640(secondrun).csv');
x_data = sixthpks(:,1);
y_data = sixthpks(:,2:end);
%Plotting
figure(1)
hold on
for pltcount = 1:size(y_data,2);
plot(x_data, y_data(:,pltcount),'Color',[pltcount/size(y_data,2),0,0]);
end
xlabel('Wavenumber (cm^{-1})');
ylabel('Absorbance');
hold off
%Fitting
for j = 1:size(y_data,2);
f = fit(x_data,y_data(:,j),'gauss2');
plot(f,x_data,y_data);
end
For context, the file being read in has wavenumbers in the first column and spectral data in the subsequent columns. The plotting section of code just produces the plot(attached) and the fitting section of code is where I am struggling.
The feature can be fitted by a two-term gaussian model, however there is no way to store the information about the fit which I need. I can't use the curve fitting toolbox (to the best of my knowledge) as it only deals with single data sets, I essentially need the fit to write it's variables into a seperate variable matrix, i.e the amplitude a1 and a2 used in the fit, should be written to variables a2 and a2 so I can plot them outside of the loop and the peak central location b1 and b2 be written to seperate variables for plotting.
Am I missing an easier way to analyse this in matlab?
![Shoulder_peak@1603.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/202830/Shoulder_peak@1603.png)
0 Comments
Answers (1)
Jeff Miller
on 6 Feb 2019
If you are just trying to save the fit outputs, it seems like this should work:
saveme = cell(size(y_data,2),1) % before the loop where you do the fitting
and
saveme{j} = f; % inside the loop where you do the fitting, after each fit
Then after the fitting loop is finished saveme will hold the fitting object produced by each fit.
HTH--not sure I really understood the question.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!