Clear Filters
Clear Filters

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

1 view (last 30 days)
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 Comments
SilvanaZ
SilvanaZ on 25 Jan 2024
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 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.

Sign in to comment.

Accepted Answer

Adam Danz
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)
  1 Comment
SilvanaZ
SilvanaZ on 26 Jan 2024
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!