loopsplotlegendscilab

SCILAB Adding legend in a plot generated by a loop


I´d like to know how to add legend in a plot generated by a loop in SciLab. There are 4 loops and each loop has a different legend (and colors).

I tried the code bellow. However, gives me the 4 legends but with the same color (in the case red).

inta=read("H_intacta_num.txt",-1,1500)
d1=read("H_dano1_num.txt",-1,1500)
d2=read("H_dano2_num.txt",-1,1500)
d3=read("H_dano3_num.txt",-1,1500)
leia1=read("Dados_MagN1.txt",-1,3);
frequ=leia1(:,1)

\\loop1 - red (label: healthy)
for i=48:94
    plot(frequ,inta(i,:),'r')
end

\\loop 2 - blue (label: Damaged_L1)
for j=48:94
    plot(frequ,d1(j,:),'b')
end

\\loop 3 - black (label: Damaged_L2)
for j=37:72
    plot(frequ,d2(j,:),'k')
end

\\loop 4 - ciano (label: Damaged_L3)
for j=36:70
    plot(frequ,d3(j,:),'c')
end

ylabel("Amplitude (m/s^2)/N")
xlabel("Frequency (Hz)")
p=legend(['Healthy';'Damaged_L1';'Damaged_L2';'Damaged_L3'],[2])


Solution

  • Here legend will consider the first 4 polylines in the stack, hence red ones. You can change the links this way (btw line comment is // not \\)

    //loop1 - red (label: healthy)
    for i=48:94
        plot(frequ,inta(i,:),'r')
    end
    h(1)=gce()
    
    //loop 2 - blue (label: Damaged_L1)
    for j=48:94
        plot(frequ,d1(j,:),'b')
    end
    h(2)=gce()
    
    //loop 3 - black (label: Damaged_L2)
    for j=37:72
        plot(frequ,d2(j,:),'k')
    end
    h(3)=gce()
    
    //loop 4 - ciano (label: Damaged_L3)
    for j=36:70
        plot(frequ,d3(j,:),'c')
    end
    h(4)=gce()
    
    ylabel("Amplitude (m/s^2)/N")
    xlabel("Frequency (Hz)")
    p=legend(h, ['Healthy';'Damaged_L1';'Damaged_L2';'Damaged_L3'],[2])