string to argument in legend
13 views (last 30 days)
Show older comments
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 Comments
the cyclist
on 3 Apr 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')
Answers (1)
Walter Roberson
on 3 Apr 2020
for i=0:Nint
legI(i+1) = "$${\mathcal X}_" + Nint-i + "$$";
end
legend(legI)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!