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
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
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
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