How can i insert a variable value (number) into modified figure legend

I'm trying to insert a variable value (number) in the figure legend, but i'm modifying the legend text as shown in the code snippet below, i tried num2str but i couldn't get it right. The figure contains two plots, The variable value would be inserted between the parenthesis after the text "Linear Fitting"
legend({'\color{blue} Test Results (G_{coh} = 1.8)','\color{red} *Linear Fitting ()*'},'Location','northoutside','Orientation','horizontal','FontWeight','bold','FontSize',10)
Thanks in advance

 採用された回答

Star Strider
Star Strider 2016 年 8 月 16 日

1 投票

I’m not quite certain what you want, but this will produce the legend I believe you want:
figure(1)
plot(1:10, rand(1, 10))
value = pi;
legend(sprintf('\\color{blue} Test Results (G_{coh} = 1.8)\n\\color{red} *Linear Fitting (%.2f)*', value),'Location','northoutside','Orientation','horizontal','FontWeight','bold','FontSize',10)

2 件のコメント

Mahmoud Sedahmed
Mahmoud Sedahmed 2016 年 8 月 16 日
My figure contains 2 plots, so i modified your line a little bit, and it provided the required results. Thanks a lot.
Star Strider
Star Strider 2016 年 8 月 16 日
My pleasure.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 8 月 16 日

1 投票

Try sprintf():
string1 = sprintf('The number is %.4f', someNumber);
string2 = sprintf('The other number is %.4f', someOtherNumber);
legend(string1, string2);

カテゴリ

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by