string to argument in legend

5 visualizaciones (últimos 30 días)
Ivan Perez Avellaneda
Ivan Perez Avellaneda el 3 de Abr. de 2020
Comentada: Ivan Perez Avellaneda el 3 de Abr. de 2020
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 comentarios
the cyclist
the cyclist el 3 de Abr. de 2020
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 el 3 de Abr. de 2020
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.

Iniciar sesión para comentar.

Respuestas (1)

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

Iniciar sesión para comentar.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by