Matlab: Data from Best Fit Lines into Legend

Click For Summary

Discussion Overview

The discussion revolves around how to display polynomial fit equations in the legend of a MATLAB plot. Participants explore methods to extract and format data from polyfit results for inclusion in graph legends.

Discussion Character

  • Technical explanation, Conceptual clarification, Homework-related

Main Points Raised

  • One participant describes their process of plotting data from Excel in MATLAB and seeks advice on displaying polyfit results in the legend.
  • Another participant suggests that adding trendlines in Excel is straightforward, implying that it may be easier than in MATLAB.
  • A participant acknowledges their proficiency in Excel but expresses a desire to learn more about MATLAB graphing techniques.
  • One participant recommends using the num2str command to convert polyfit output into a string format suitable for the legend.
  • A later reply confirms that the suggestion to use num2str was helpful.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method, but there is agreement on the utility of the num2str command for formatting polyfit results.

Contextual Notes

Some assumptions about familiarity with MATLAB and its functions may not be explicitly stated. The discussion does not resolve how to integrate the formatted string into the legend function.

Who May Find This Useful

Individuals learning MATLAB for data visualization, particularly those transitioning from Excel or seeking to enhance their graphing capabilities.

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

  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 12 ·
Replies
12
Views
7K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
2
Views
3K