string to argument in legend

조회 수: 5 (최근 30일)
Ivan Perez Avellaneda
Ivan Perez Avellaneda 2020년 4월 3일
댓글: Ivan Perez Avellaneda 2020년 4월 3일
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
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')
Ivan Perez Avellaneda
Ivan Perez Avellaneda 2020년 4월 3일
yes, you're right, but I don't know how to transform the first into the second one. I used the function eval(legII), but no success.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 4월 3일
for i=0:Nint
legI(i+1) = "$${\mathcal X}_" + Nint-i + "$$";
end
legend(legI)
  댓글 수: 1
Ivan Perez Avellaneda
Ivan Perez Avellaneda 2020년 4월 3일
thank you, it works perfect. Just needed to put
legend(legI,'Interpreter','latex')
and that's it.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by