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 件のコメント
SilvanaZ
SilvanaZ 2024 年 1 月 25 日
Hi Adam,
please save the inputForApp file to your computer. Copy its path via right click "Copy as Path". (I am working on a Windows computer, copying the path this way surrounds the file location with this symbol "filepath". the dividing symbol is the backslash \. )
You enter this into the text box: File Path to Results Table. Then press the Read Table button.
Then you press the IMMS1 button, select the threshold (0.0016) and then press the fit button once: This should result in the graph seen in the first picture. Then just keep on pressing the fit button as often as you want. You should be able to see the plot of the fit expand (like in the second picture).
I hope this works for you too. Let me know if there are any other questions.
Jon
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
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)
  1 件のコメント
SilvanaZ
SilvanaZ 2024 年 1 月 26 日
Dear Adam,
thank you for this answer.
I am not sure why the x-values would be selected based on the axes limits (in my mind this makes no sense...) but both options work.
I will accept the answer, because it fixes my issue for the time being. I just want to point out that when I zoom out in the figure the original issue persists (which makes sense).
Thank you for your help! This has saved me a lot of time :-)
BR
Silvana

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by