How to pass a variable with latex interpreter in the legend?
조회 수: 49 (최근 30일)
이전 댓글 표시
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');
댓글 수: 0
채택된 답변
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
Walter Roberson
2023년 12월 4일
lgd = legend(["$M>3$", "$\tau = $ " + a_Fit + " $p = $ " + r_Fit], 'interpreter', 'latex');
추가 답변 (1개)
Dyuman Joshi
2023년 12월 4일
편집: Dyuman Joshi
2023년 12월 4일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!