MATLAB App Designer: How to display a scatter plot on the UIaxes with linear fit and equation of line ?
3 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
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)
plot(str,Ec1,'o',str1,f,'-')
5 Comments
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))
plot(str,Ec1,'o',str1,f,'-')
ax = gca
text(ax,300,26,sprintf('y = %.5fx + %.2f',p(1),p(2)))
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!