MATLAB Matlab: Data from Best Fit Lines into Legend

AI Thread Summary
The discussion focuses on plotting data in MATLAB and displaying the polyfit equations in the graph legend. The user successfully retrieves data from Excel and calculates the best fit lines using polyfit but is unsure how to incorporate the resulting equations into the legend. A suggestion is made to use the num2str command to convert the polyfit output into a string format suitable for the legend. The user expresses gratitude for this solution, indicating it meets their needs. This exchange highlights the transition from Excel to MATLAB for data visualization tasks.
SeannyBoi71
Messages
84
Reaction score
0
Hey all, I am plotting some data using MATLAB and have a question.

Basically, this is what I'm trying to do. I am calling a bunch of data from Excel, plotting this data in MATLAB (it gives me two lines), and then calculating the polyfit values for each line so I can find the equation y = mx +b for each one. I have no problem Doing all of this, but what I want to do next is display the polyfit data in the legend of the graph. I'm confused if this is possible with the legend function.

10aYi.jpg


So in other words where it says 'Best Fit of Reflection 1' and 'Best Fit of Reflection 2' I want to somehow place the data calculated by polyfit in the form y = mx +b. Any suggestions? Thanks in advance.
 
Physics news on Phys.org
This is simple in Excel, where you say you started. Chart the data, then pulldown the chart menu, select "add trendline" under options you can select to show the equations.
 
I was aware of this method being the easy way out :-p I am pretty proficient with Excel and have only recently started using Matlab, so I was trying to familiarize myself a little bit more with graphing this way.
 
Just use the num2str command on the values that polyfit returns
 
Ok this is what I was looking for, thank you.
 

Similar threads

Back
Top