How can I add a trendline to a scattergraph of multiple data sets?

조회 수: 14 (최근 30일)
Lala0099
Lala0099 2019년 11월 6일
댓글: Star Strider 2024년 2월 6일
Hey Everyone,
I have uploaded my code and a data set that I am trying to process.
So I created a code that works well enough for my purposes. The trouble that I am having is that I need a scatter graph, with all the points that i included in the plotting section. But i also need a trendline for each plot within that one graph. And I need to find the gradient of the trendline for each data set and store it in a variable.
I tried to use, this command, but i am not sure if it the same as a trendline in excel and also cannot get the gradient for this. I would appreciate any help!!
lsline

채택된 답변

Star Strider
Star Strider 2019년 11월 6일
The lsline function needs a bit of help to give up its secrets.
Try this:
figure
scatter(rand(1,10), rand(1,10))
hold on
scatter(rand(1,20), rand(1,20))
hold off
h = lsline;
for k = 1:numel(h)
B = polyfit(h(k).XData, h(k).YData,1);
slope(k) = B(1);
intercept(k) = B(2);
end
Experiment with your own data and plots to get the result you want.
  댓글 수: 11
Sena
Sena 2024년 2월 6일
Hello, I wanted to do the same thing and used your code.
How to I tweak this code so it is using my data points? Everytime I try to add it into the line, B = polyfit(h(k).XData, h(k).YData,1); with dot indexing it does not work, it gives me this error:
Unrecognized method,
property, or field
'VariableName' for class
'matlab.graphics.primitive.Line'.
Star Strider
Star Strider 2024년 2월 6일
@Sena — I would have to know more about your particular problem. I just tested it again using R2023b and it still works.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by