Odd behaviour when displaying fit results in UIAxes in App Designer (Matlab R2023b)
1 view (last 30 days)
Show older comments
Hi,
I am currently working on an app in the app designer and I keep having odd behaviour when displaying my fit results in the UIAxes of my app.
When I press the fit button, the fit is displayed as expected.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1598846/image.jpeg)
When I click the button again the fit is extended:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1598851/image.jpeg)
This is the code I use:
yData and xData are vectors of the size 200x1 each.
% Button pushed function: FitButton
function FitButtonPushed(app, event)
[~, locs, w, ~] = findpeaks(app.yData,app.xData,'Threshold',app.threshold);
app.smoothFit = fit(app.xData,app.yData,'smoothingspline');
% determine height
hgt = zeros(length(locs), 1);
for k = 1:length(locs)
hgt(k) = feval(app.smoothFit,locs(k));
end
app.x = linspace(app.xData(20),app.xData(end),100000);
for n = 1:length(locs)
Gauss(n,:) = hgt(n)*exp(-((app.x - locs(n))/w(n)).^2);
end
plot(app.UIAxesFit,app.smoothFit)
hold(app.UIAxesFit, 'on')
plot(app.UIAxesFit,app.x,Gauss,'--')
legend(app.UIAxesFit,'off')
xlabel(app.UIAxesFit,'CCS (Ų)')
ylabel(app.UIAxesFit,'Intensity')
hold(app.UIAxesFit, 'off')
end
I would be greatful for suggestions on why this is happening.
Thanks!
Silvana
7 Comments
Jon
on 25 Jan 2024
I'm assuming that your fit function is from the Curve Fitting Toolbox. Unfortunately I don't have that toolbox, so I can not run your example.
Accepted Answer
Adam Danz
on 25 Jan 2024
When plotting a cfit object using plot(ax,cfit) the x-values for the line are selected based on the axes limits. Every time you run the FitButtonPushed callback function, the axes limits slightly expand. Therefore, the extent of the fit line also expands.
To prevent this from happening, freeze the XLim values prior to plotting the lines.
Option 1
This sets the XLimMode to manual which freezes the X-limits.
% Add this line
app.UIAxesFit.XLimMode = 'manual';
plot(app.UIAxesFit,localFit)
Option 2
This also sets the XLimMode to manual which freezes the X-limits but returns the original XLimMode values after the callback is complete.
% Add these two lines
restoreXLimMode = onCleanup(@()set(app.UIAxesFit,'XLimMode',app.UIAxesFit.XLimMode));
app.UIAxesFit.XLimMode = 'manual';
plot(app.UIAxesFit,localFit)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!