Odd behaviour when displaying fit results in UIAxes in App Designer (Matlab R2023b)

2 ビュー (過去 30 日間)
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));
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);
hold(app.UIAxesFit, 'on')
xlabel(app.UIAxesFit,'CCS (Ų)')
hold(app.UIAxesFit, 'off')
I would be greatful for suggestions on why this is happening.
  7 件のコメント
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 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';
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';
  1 件のコメント
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 :-)


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by