How to pass a variable with latex interpreter in the legend?

55 ビュー (過去 30 日間)
Vikash Pandey
Vikash Pandey 2023 年 12 月 4 日
コメント済み: Dyuman Joshi 2023 年 12 月 4 日
I am mentioning the relevant part of the code below. r_Fit is supposed to be a variable. How do I pass its value in the legend with a mathematical expression, $\tau = $ attached to it? Please help.
a_Fit = output_parameters(1);
r_Fit = output_parameters(2);
r_Fit_Str = num2str(r_Fit);
N_AS_Above3_Fit = a_Fit.*N_Days.^(-r_Fit);
figure(002);
plot(N_Days, N_AS_Above3, '+', 'LineWidth', 3, 'MarkerSize', 14, 'MarkerEdgeColor', 'r');
hold on;
plot(N_Days, N_AS_Above3_Fit, 'k', 'LineWidth', 3);
lgd = legend({'$M>3$', '$\tau = $ r_Fit'}, 'interpreter', 'latex');

採用された回答

Walter Roberson
Walter Roberson 2023 年 12 月 4 日
lgd = legend(["$M>3$", "$\tau = $ " + r_Fit], 'interpreter', 'latex');
This will convert r_Fit according to internal (and seemingly inconsistent) rules about how many digits should be converted.
If you need more certainty in the formatting, use compose
  3 件のコメント
Vikash Pandey
Vikash Pandey 2023 年 12 月 4 日
編集済み: Vikash Pandey 2023 年 12 月 4 日
One more question. I wish to add another variable in the second legend. I tried this, but it did not worked. It only prints the first legend and the first part of the second legend.
lgd = legend(["$M>3$", "$\tau = $ " + a_Fit "$p = $ " + r_Fit], 'interpreter', 'latex');
Walter Roberson
Walter Roberson 2023 年 12 月 4 日
lgd = legend(["$M>3$", "$\tau = $ " + a_Fit + " $p = $ " + r_Fit], 'interpreter', 'latex');

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

その他の回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2023 年 12 月 4 日
編集済み: Dyuman Joshi 2023 年 12 月 4 日
Use compose to generate the string, modify it and use it as an input to legend().
Note that added \ before \tau.
str = compose(['$\\tau = $ %f'], r_fit)
str = [{'$M>3$'} str];
legend(str, 'interpreter', 'latex');
Change the modifier according to the data-type and according to the number of digits you want to show.
  4 件のコメント
Vikash Pandey
Vikash Pandey 2023 年 12 月 4 日
Thanks, Dyuman. it worked.
Dyuman Joshi
Dyuman Joshi 2023 年 12 月 4 日
You're welcome!

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

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by