Using Curve Fitter, computing Local Maximum (y) and corresponding (x) and export x y of fitted curve to external format (Excel)

1 view (last 30 days)
I have a dataset: matlab_test_data.xlsx which I have uploaded to MATLAB (Home > Upload > matlab_test_data.xlsx).
I plotted the data using the following command:
%: Read the excel file using readtable function
rawTable = readtable('matlab_test_data.xlsx','Sheet','Sheet1');
x = rawTable.Days; %: get the excel column, Header1 (header name)
y = rawTable.Sucrose; %: get the excel column, Header2 (header name)
figure;
plot(x,y);
I then used the Curve Fitter (Apps > Curve Fitter > Select Data > Select Fitting Data)
Fitname: Trendline
X data: x
Y data: y
(Ignoring NaNs in data)
Polynomial Degree: 3
Robust: Off
Followed by Export > Export to Figure and Export to Workspace
How can I find the Local Maximum (y): 12.0799 and corresponding (x): 228.885 from the Fitted Curve (Trendline) in Figure 2 and extract (x,y) values of the Fitted Curve to Excel.

Accepted Answer

Matt J
Matt J on 31 Jul 2022
Edited: Matt J on 31 Jul 2022
If all you're after is the local max, I would skip the curve fitting app and use polyfit.
keep=~(isnan(x)|isnan(y));
x=x(keep);
y=y(keep);
p=polyfit(x,y,3);
dp=polyder(p);
ddp=polyder(dp);
xs=roots(dp);
xs(abs(imag(xs))>1e-6)=0; %get rid of imaginary junk
xmax=xs(polyval(ddp,xs)<0)
xmax = 229.1196
ymax=polyval(p,xmax)
ymax = 12.0800
writematrix([xmax,ymax],'filename.xlsx')
  6 Comments
Hansraj
Hansraj on 31 Jul 2022
I'm having trouble with the below command and recommended xlwrite and writematrix to export the (x,y) data to excel format.
x=get(gco,'Xdata')
y=get(gco,'Ydata')
Hansraj
Hansraj on 31 Jul 2022
To those who may be interested, please read
x=get(gco,'Xdata')
y=get(gco,'Ydata')
Followed by:
writematrix([x(:), y(:)],'filename3.xlsx')

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!