MATLAB App Designer: How to display a scatter plot on the UIaxes with linear fit and equation of line ?

3 views (last 30 days)
I am migrating my script to App Designer.
Below script (normal script) is ploting a scatter plot with displayed linear fit and equation of line (figure 1).
Normal script:
str = [107.0176, 256.7374, 257.2012, 441.7806, 420.5170, 602.7027, 591.8610, 756.6289, 896.9324];
Ec1 = [23.8829, 19.0262, 28.8904, 22.3871, 29.4871, 24.5282, 29.3028, 26.3303, 24.5477];
createfigure(str,Ec1)
Figure 1:
Above script cannot be written as such when I am migrating to App Designer. Thus I did rewrite the script (App designer script) to get the best possible same outcome but only able to generate as in figure 2. The generated figure has no equation of line displayed and the linear fit need long script for it to be plotted.
App designer script:
str = [107.0176, 256.7374, 257.2012, 441.7806, 420.5170, 602.7027, 591.8610, 756.6289, 896.9324];
Ec1 = [23.8829, 19.0262, 28.8904, 22.3871, 29.4871, 24.5282, 29.3028, 26.3303, 24.5477];
p = polyfit(str,Ec1,1);
f = polyval(p,str);
plot(app.UIAxes3,str,Ec1,'o',str,f,'-')
hold (app.UIAxes3,'on')
legend(app.UIAxes3,'show')
hold (app.UIAxes3,'off')
Figure 2:
Seek your kind help on this very issue.
disclaimer: this script is the simplified version of full script.

Answers (1)

VBBV
VBBV on 3 Nov 2022
str = [107.0176, 256.7374, 257.2012, 441.7806, 420.5170, 602.7027, 591.8610, 756.6289, 896.9324];
Ec1 = [23.8829, 19.0262, 28.8904, 22.3871, 29.4871, 24.5282, 29.3028, 26.3303, 24.5477];
p = polyfit(str,Ec1,1);
f = polyval(p,str)
f = 1×9
24.4321 24.8096 24.8108 25.2763 25.2227 25.6821 25.6548 26.0703 26.4241
str1 = linspace(min(str),max(str),9)
str1 = 1×9
107.0176 205.7570 304.4963 403.2357 501.9750 600.7143 699.4537 798.1930 896.9324
plot(str,Ec1,'o',str1,f,'-')
  5 Comments
VBBV
VBBV on 3 Nov 2022
Edited: VBBV on 3 Nov 2022
To find equation, use poly2sym and to display equation in the plot use annotation or text functions using the axes handles
VBBV
VBBV on 3 Nov 2022
str = [107.0176, 256.7374, 257.2012, 441.7806, 420.5170, 602.7027, 591.8610, 756.6289, 896.9324];
Ec1 = [23.8829, 19.0262, 28.8904, 22.3871, 29.4871, 24.5282, 29.3028, 26.3303, 24.5477];
p = polyfit(str,Ec1,1);
f = polyval(p,str);
str1 = linspace(min(str),max(str),9);
y = (vpa(poly2sym([p]),3))
y = 
plot(str,Ec1,'o',str1,f,'-')
ax = gca
ax =
Axes with properties: XLim: [100 900] YLim: [18 30] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Show all properties
text(ax,300,26,sprintf('y = %.5fx + %.2f',p(1),p(2)))

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!