0
votes

Suppose I want to plot a data along with it's fitted curve in a single plot. In the legend items the first item should be 'data', the first line of the second item should be 'fitted line', the second line of second item should contain fit parameters 'a' and 'b' or slope and intercept.

I tried the following, but it doesn't work

[f,gof] = fit(x,y,linearfittype);
legend(data,{'fitted curve',['a=' num2str(f.a)],['b=' num2str(f.b)],['R-squared=' num2str(gof.rsquare)]});

I tried the suggestion in the following page, but none of them work http://www.mathworks.com/matlabcentral/newsreader/view_thread/96715

I also want a line break or vertical space between the two legend items

1
Can you show the result you obtain using the sprintf approach? I just posted an answer but I see that you tried it already. - Benoit_11

1 Answers

0
votes

Using sprintf should create line breaks (using \n) as you want. For instance try this:

T = sprintf( 'a = %0.2f\nb = %0.2f\nR-squared = %0.2f',f.a,f.b,gof.rsquare); 
legend(data,{'fitted curve',T});