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

Lala0099

### Lala0099 (view profile)

님이 질문을 제출함. 6 Nov 2019
최근 활동 Star Strider

### Star Strider (view profile)

님이 댓글을 추가함. 7 Nov 2019
Star Strider

### Star Strider (view profile)

님이 답변을 채택함.
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

로그인 to comment.

## 답변 수: 1

Star Strider

### Star Strider (view profile)

님의 답변 6 Nov 2019
채택된 답변

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.

표시 이전 댓글 수: 6
Lala0099

### Lala0099 (view profile)

7 Nov 2019
Hey sorry, i did attach it but for some reason it did not upload it. I tried to upload the files again, zipped them, but it did not allow me.
So I only uploaded the matlab file.
The table tha i am using is just massive with several sheets, and have loads of data points.
Lala0099

### Lala0099 (view profile)

7 Nov 2019
I gave it another go and it worked now ! Sorry for the confusion!! I very appreciate your help! Thank you!
Star Strider

### Star Strider (view profile)

7 Nov 2019
As always, my pleasure!

로그인 to comment.

Translated by