string to argument in legend
조회 수: 5 (최근 30일)
이전 댓글 표시
I am trying to generate the legend for N plots. Each plot uses latex code. For example, in the following picture there are 10 sets and I inserted legends for 4 sets. I did
it manually using
legend(strcat('$${\mathcal X}_',num2str(3), '$$'), strcat('$${\mathcal X}_',num2str(2), '$$'), strcat('$${\mathcal X}_',num2str(1), '$$'),strcat('$${\mathcal X}_',num2str(0), '$$'),'Interpreter','latex')
So I wanted to generate the argument of the legend as a string. I did
for i=0:Nint-1
legI = strcat("strcat('$${\mathcal X}_',num2str(",num2str(Nint-i),"), '$$')");
legII=strcat(legII,legI,',');
end
legII=strcat(legII,strcat("strcat('$${\mathcal X}_',num2str(",num2str(0),"), '$$')"));
where Nint=10 (the number of sets). This gives me
legII =
"strcat('$${\mathcal X}_',num2str(10), '$$'),strcat('$${\mathcal X}_',num2str(9), '$$'),strcat('$${\mathcal X}_',num2str(8), '$$'),strcat('$${\mathcal X}_',num2str(7), '$$'),strcat('$${\mathcal X}_',num2str(6), '$$'),strcat('$${\mathcal X}_',num2str(5), '$$'),strcat('$${\mathcal X}_',num2str(4), '$$'),strcat('$${\mathcal X}_',num2str(3), '$$'),strcat('$${\mathcal X}_',num2str(2), '$$'),strcat('$${\mathcal X}_',num2str(1), '$$'),strcat('$${\mathcal X}_',num2str(0), '$$')"
then I did
legend(convertStringsToChars(legII),'Interpreter','latex');
but it didn't interepret the latex nor give me the Nint=10 legends.
댓글 수: 2
the cyclist
2020년 4월 3일
Here is why what you are doing doesn't work. Take the simplified case of only trying to make the legend with X0. The "manual" version is this code:
legend(strcat('$${\mathcal X}_',num2str(0), '$$'),'Interpreter','latex')
But your "automated" version evaluates to this:
legend('strcat('$${\mathcal X}_',num2str(0), '$$')','Interpreter','latex')
답변 (1개)
Walter Roberson
2020년 4월 3일
for i=0:Nint
legI(i+1) = "$${\mathcal X}_" + Nint-i + "$$";
end
legend(legI)
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!