Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab: Data from Best Fit Lines into Legend

  1. Mar 13, 2012 #1
    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.


    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.
  2. jcsd
  3. Mar 13, 2012 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.
  4. Mar 13, 2012 #3
    I was aware of this method being the easy way out :tongue: 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.
  5. Mar 13, 2012 #4


    User Avatar
    Science Advisor
    Gold Member

    Just use the num2str command on the values that polyfit returns
  6. Mar 13, 2012 #5
    Ok this is what I was looking for, thank you.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook