How do I name the trendline in the legend? Also, how can I get the trend line equation on the plot?
9 次查看(过去 30 天)
显示 更早的评论
回答(2 个)
Bjorn Gustavsson
2019-3-19
Maybe:
if c(2) > 0
equation_string = sprintf('%4.3f x + %4.3f',c(1),c(2));
elseif c(2) < 0
equation_string = sprintf('%4.3f x - %4.3f',c(1),abs(c(2)));
else
equation_string = sprintf('%4.3f x',c(1));
end
legend('Data',['Best linear fit: ',equation_string])
text(12,43,equation_string)
HTH
0 个评论
Star Strider
2019-3-19
It is not possible to run an image of your code.
Try this:
Voltage = linspace(10.45, -1.4, 8);
GaugePressure = linspace(3353, 0, 8);
c = polyfit(Voltage, GaugePressure, 1);
y_est = polyval(c, Voltage);
figure
plot(Voltage, GaugePressure, '--bs')
hold on
plot(Voltage, y_est, 'r--')
hold off
legend('Data', 'TrendLine', 'Location','best')
Add your title and axis label calls.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!