How can I use a string in legend()?
이전 댓글 표시
I have the following string:

Now I want to use this string to produce a legend to my figure. So I use
and get this:

But I would like to have this,

which I got by using the following command:
Tthis is exactly the string Legend copied into the function legend().
How can I use directly the string Legend, without copying it to the function legend()?
Edit: I forgot the following code sniplet:
p=zeros(2*length(HG),1);
hold on;
for iter2 = 1:length(HG)
p(2*iter2-1)=plot(freq,real(n(:,iter2)), 'color', cc(iter2,:));
p(2*iter2)= plot(freq,imag(n(:,iter2)),'--', 'color', cc(iter2,:));
end
I use the variable p to skip all dotted lines in the legend.
댓글 수: 3
Ameer Hamza
2020년 10월 26일
Why do you want to use the first syntax? It is not supported in MATLAB. The second syntax is the correct MATLAB's way of drawing the legends.
Marcel345614
2020년 10월 26일
채택된 답변
추가 답변 (1개)
Alan Stevens
2020년 10월 26일
You could use
Legend = ['HS+HG = 0.5+5' ;'HS+HG = 1+5'];
legend(Legend)
But make sure there are the same number of characters (including spaces) in both strings.
댓글 수: 2
Ameer Hamza
2020년 10월 26일
Following alternatives does not require that character arrays have equal lengths.
Legend = {'HS+HG=0.5+5';'HS+HG=1+5'};
legend(Legend)
or
Legend = ["HS+HG=0.5+5";"HS+HG=1+5"];
legend(Legend)
Penghao Duan
2021년 4월 18일
Thanks, that is helpful
카테고리
도움말 센터 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!