Search code examples
matlabplotlegendmatlab-figuresubplot

How to change the legend string for different subplots?


I would like to add a legend to every subplot as shown below

enter image description here

each legend is theta_(1/2)=5, theta_(1/2)= 10, theta_(1/2)=15,...,theta_(1/2)=60. How can I change the number for each legend?

Q = [16,32,64,128,256,512,1024];

VEC_Beam_5 = [113.2659   69.7204   51.8739   34.5033   21.6538   10.9459    5.9488];   % Coverage area of Theta = 5
VEC_Beam_10 = [117.5491   76.1452   53.3016   38.3105   23.0815   13.5634    6.9007];  % Coverage area of Theta = 10
VEC_Beam_15 = [117.5491   76.1452   53.3016   38.3105   23.0815   13.5634    6.9007];  % Coverage area of Theta = 15
VEC_Beam_20 = [117.7870   76.1452   53.3016   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 20
VEC_Beam_25 = [117.7870   76.6211   54.2534   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 25
VEC_Beam_30 = [117.7870   76.6211   54.2534   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 30
VEC_Beam_35 = [117.7870   76.6211   54.2534   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 35
VEC_Beam_40 = [117.7870   76.6211   54.2534   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 40
VEC_Beam_45 = [117.7870   76.6211   54.2534   38.7864   23.0815   13.5634    6.9007];  % Coverage area of Theta = 45
VEC_Beam_50 = [117.7870   76.6211   54.2534   38.7864   23.0815   13.5634    6.9007];  % Coverage area of Theta = 50
VEC_Beam_55 = [117.7870   76.6211   54.2534   38.7864   23.0815   13.5634    6.9007];  % Coverage area of Theta = 55
VEC_Beam_60 = [117.7870   76.6211   54.2534   38.7864   23.0815   13.5634    6.9007];  % Coverage area of Theta = 60

VEC_Beam = [VEC_Beam_5;VEC_Beam_10;VEC_Beam_15;VEC_Beam_20;VEC_Beam_25;VEC_Beam_30;VEC_Beam_35;VEC_Beam_40;VEC_Beam_45;VEC_Beam_50;VEC_Beam_55;VEC_Beam_60];
figure

cmap = jet(12);

for k1 = 1:12
    subplot(2,6,k1);
    plot(log2(Q),VEC_Beam(k1,:), 'Color', cmap(k1, :),'LineWidth',2, 'Marker','O');
    xticks(log2(Q))
    xticklabels(string(Q))
    xlabel('Q');
    ylabel('Coverage area');
    grid on;
end

sgtitle('QAM 16');

Solution

  • Just alter your string within a loop. I suggest using sprintf() for clarity:

    for ii = 1:12
        % your plot
        legend_str = sprintf('\\theta_(1/2) = %d\\circ', 5 * ii)
        legend(legend_str)
    end
    

    Thanks to Sardar Usama for the LaTeX corrections.