How can I include the legends within the for loop and how can I set the xticks in MATLAB?

조회 수: 2 (최근 30일)
I have below the following data
Q = [16,32,64,128,256,512,1024];
VEC_5 = [0.2380 0.2380 0.2380 0.2380 0.2380 0.2380 0.2380];
VEC_10 = [1.1898 1.1898 0.2380 0.2380 0.2380 0.2380 0.2380];
VEC_15 = [2.1416 2.1416 2.1416 1.1898 1.1898 0.2380 0.2380];
VEC_20 = [4.9970 3.0934 2.1416 2.1416 2.1416 1.1898 1.1898];
VEC_25 = [4.9970 4.9970 4.9970 3.0934 2.1416 2.1416 1.1898];
VEC_30 = [8.8043 5.9488 4.9970 4.9970 3.0934 2.1416 1.1898];
VEC_35 = [10.7079 8.8043 6.9007 4.9970 4.9970 2.1416 2.1416];
VEC_40 = [14.5152 10.7079 8.8043 6.9007 4.9970 3.0934 2.1416];
VEC_45 = [16.4188 11.6597 10.7079 8.8043 4.9970 4.9970 2.1416];
VEC_50 = [19.2742 14.5152 10.7079 8.8043 5.9488 4.9970 2.1416];
VEC_55 = [21.1779 16.4188 13.5634 10.7079 6.9007 4.9970 2.1416];
VEC_60 = [23.5574 16.4188 14.5152 10.7079 8.8043 4.9970 2.6175];
I drew them with semilogx function in Matlab as shown below:
as demonstrated in the code below:
VEC = [VEC_5;VEC_10;VEC_15;VEC_20;VEC_25;VEC_30;VEC_35;VEC_40;VEC_45;VEC_50;VEC_55;VEC_60];
figure
cmap = jet(12);
hold on
for k = 1:12
semilogx(Q,VEC(k,:), 'Color', cmap(k, :),'LineWidth',2,'MarkerEdgeColor','b');
end
hold off
xticks(Q)
xlabel('Q order');
ylabel('Coverage area');
legend('\theta_1_/_2 = 5','\theta_1_/_2 = 10','\theta_1_/_2 = 15','\theta_1_/_2 = 20','\theta_1_/_2 = 25','\theta_1_/_2 = 30','\theta_1_/_2 = 35','\theta_1_/_2 = 40','\theta_1_/_2 = 45','\theta_1_/_2 = 50','\theta_1_/_2 = 55','\theta_1_/_2 = 60')
grid on;
So, how can I include legends with the for loop instead of writing it outside? and the values in the x-axis are too close to each other how can I separate them to something like below? and how can I add markers on the lines?

채택된 답변

Matt J
Matt J 2022년 9월 25일
편집: Matt J 2022년 9월 25일
So, how can I include legends with the for loop instead of writing it outside?
You are assuming here that the for loop is the only way to automatically generate the legend strings. Not so:
Q = [16,32,64,128,256,512,1024];
VEC_5 = [0.2380 0.2380 0.2380 0.2380 0.2380 0.2380 0.2380];
VEC_10 = [1.1898 1.1898 0.2380 0.2380 0.2380 0.2380 0.2380];
VEC_15 = [2.1416 2.1416 2.1416 1.1898 1.1898 0.2380 0.2380];
VEC_20 = [4.9970 3.0934 2.1416 2.1416 2.1416 1.1898 1.1898];
VEC_25 = [4.9970 4.9970 4.9970 3.0934 2.1416 2.1416 1.1898];
VEC_30 = [8.8043 5.9488 4.9970 4.9970 3.0934 2.1416 1.1898];
VEC_35 = [10.7079 8.8043 6.9007 4.9970 4.9970 2.1416 2.1416];
VEC_40 = [14.5152 10.7079 8.8043 6.9007 4.9970 3.0934 2.1416];
VEC_45 = [16.4188 11.6597 10.7079 8.8043 4.9970 4.9970 2.1416];
VEC_50 = [19.2742 14.5152 10.7079 8.8043 5.9488 4.9970 2.1416];
VEC_55 = [21.1779 16.4188 13.5634 10.7079 6.9007 4.9970 2.1416];
VEC_60 = [23.5574 16.4188 14.5152 10.7079 8.8043 4.9970 2.6175];
VEC = [VEC_5;VEC_10;VEC_15;VEC_20;VEC_25;VEC_30;VEC_35;VEC_40;VEC_45;VEC_50;VEC_55;VEC_60];
figure
cmap = jet(12);
hold on
for k = 1:12
plot(log2(Q),VEC(k,:), 'Color', cmap(k, :),'LineWidth',2,'MarkerEdgeColor','b');
end
hold off
xticks(log2(Q))
xticklabels(string(Q))
xlabel('Q order');
ylabel('Coverage area');
legstr="\theta_1_/_2 ="+(5:5:60);
legend(legstr)
grid on;
  댓글 수: 5
Matt J
Matt J 2022년 9월 25일
For example,
plot(log2(Q),VEC(k,:), 'Color', cmap(k, :),'LineWidth',2,'Marker','d');

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by