Odd behaviour when displaying fit results in UIAxes in App Designer (Matlab R2023b)
조회 수: 2 (최근 30일)
이전 댓글 표시
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.
When I click the button again the fit is extended:
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
Jon
2024년 1월 25일
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.
채택된 답변
Adam Danz
2024년 1월 25일
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)
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!