Assigning legend in for-loop Matlab

16,372

Solution 1

Create a cell array to hold the legend names. Before the for loop define something like

legend_names = cell(1,27 * 14);

Then, during the loop fill the cell in:

legend_names{27*(m-1)+i} = obj.m_Core_List(i).name;

After the end set the legend:

legend(legend_names);

I might have misunderstood the indices (m vs i) and how they relate to the names, but the point is that you can pass a cell array to the legend function to create the legend at one time.

An example is:

>> legend_names=cell(1,2);
>> legend_names{1} = 'str';
>> legend_names{2} = 'str2';
>> plot(0:4,0:4)
>> hold on
>> plot(1:5,0:4)
>> legend(legend_names)

which would yieldenter image description here

Solution 2

Instead of collecting the legend string, you can just set the DisplayName-property in your plot commands. Also, you can collect the linespec arguments in a cell array to avoid code duplication, i.e.

linespec = {'--gs','--rs',... %# etc

(...) 

for i=1:1:27
        Wa_Ac = PVinv.CoreSizeModel();
        PVinv.CoreSelect(Wa_Ac,i);   
        loss_ind_core= PVinv.InductorLossModel(PVinv.m_L_Selected);


        p=plot(vin,loss_ind_core,linespec{i},'DisplayName',obj.m_Core_List(i).name);

end        

legend('show')

Solution 3

When faced with this situation, each time through the loop I add the legend string to a cell array of strings, e.g.

legstr{i} = obj.m_Core_List(i).name; 

and then display the legend once, after the end of the loop:

legend(legstr);
Share:
16,372

Related videos on Youtube

mirage
Author by

mirage

Updated on June 04, 2022

Comments

  • mirage
    mirage almost 2 years

    I have tried giving the legend in the loop but it overwrites the previously written legend, how can insert them either in if statement or either in the for loop. Confused

        clear;
            vin=10
     for m=1:1:14;  
    
            vin=vin+10
        for i=1:1:27
            Wa_Ac = PVinv.CoreSizeModel();
            PVinv.CoreSelect(Wa_Ac,i);   
            loss_ind_core= PVinv.InductorLossModel(PVinv.m_L_Selected);
    
            if(i==1)
            p=plot(vin,loss_ind_core,'--gs');
            hold on
            end
            if(i==2)
            p=plot(vin,loss_ind_core,'--rs');
            end %...till i=27
    
            legend(obj.m_Core_List(i).name);
            xlim([10e3 90e3])
            set(gca,'XTickLabel',{'10';'20';'30';'40';'50';'60';'70';'80';'90'})
            grid on
            xlabel('Vin');
            ylabel('Power loss');
        end
     end
    

    The called function

    function obj = CoreSelect(obj, WaAc)
                 obj.m_Core_Available= obj.m_Core_List(i);
                obj.m_L_Selected.m_Core = obj.m_Core_Available;
    
    end 
    
  • mirage
    mirage about 12 years
    Error:Cell array argument must be a cell array of strings. Actually i has to loop 27 times for every m value. –
  • user2032201
    user2032201 about 12 years
    @Khalid - you can use a string matrix instead of a cell (see weizmann.ac.il/matlab/techdoc/ref/legend.html for legend documentation)
  • Taylor Southwick
    Taylor Southwick about 12 years
    @Khalid where is the error coming in? I've edited it for 27 times for every m value
  • mirage
    mirage about 12 years
    Used the logic but it turned out to be the same problem in the end, it rewrites on the previous legend values
  • mirage
    mirage about 12 years
    The error was at the point when legend command is executed. Legend was not accepting the cell characters as its arguments
  • mirage
    mirage about 12 years
    It worked out, i was using the if statements to do it..But as I moved my ifelse statements outside the for loop things worked out
  • Taylor Southwick
    Taylor Southwick about 12 years
    @Khalid Make sure you are assigning strings to the cell. It sounds like your cell array is not composed of strings.
  • Bernhard
    Bernhard almost 10 years
    For future reader: the example works perfectly for me in 2013b.