Info
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
Issues with the legend of a plot with hidable lines.
    2 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
Hi,
what i tried to do is the following test-code:
                                                A=[10 20 30];
                                                M=[1 2 3; 4 5 6; 7 8 9];
                                                N=[11 12 13; 14 15 16; 17 18 19];
                                                x=0 %testing with '1' and '0'
                                                figure;
                                                hold;
                                                grid on;
                                                title('Test');
                                                xlabel('A');
                                                ylabel('matrix');
                                                for i = 1:3
                                                    p1 = plot(A, M,'--');  
                                                end
                                                if    x==1   
                                                else
                                                        for i = 1:3
                                                            p2 = plot(A, N);  
                                                        end
                                                end
                                                legend([p1(1),p2(1)],'Lager M','Lager N','Location','NorthWest')
But when i use it in my script with the 3rd (and 4th) plot being disabled, it says "Undefined function or variable "pIrc"." (refering to the Legend-Line):
                                                figure;
                                                hold;
                                                grid on;
                                                for i = 1:length(data.stepsRotationalSpeed)
                                                    pIra = plot(data.stepsPreload, data.hertzIr_A(i,:));  
                                                end
                                                for i = 1:length(data.stepsRotationalSpeed)
                                                    pIrb = plot(data.stepsPreload, data.hertzIr_B(i,:));  
                                                end
                                                if (get(handles.popupmenuC,'Value') == 1)                                                    
                                                else
                                                    for i = 1:length(data.stepsRotationalSpeed)
                                                        pIrc = plot(data.stepsPreload, data.hertzIr_C(i,:));  
                                                    end
                                                end
                                                if (get(handles.popupmenuD,'Value') == 1)                                                    
                                                else
                                                    for i = 1:length(data.stepsRotationalSpeed)
                                                        pIrd = plot(data.stepsPreload, data.hertzIr_D(i,:));  
                                                    end
                                                end
                                                legend([pIra(1),pIrb(1),pIrc(1),pIrd(1)],'A','B','C','D','Location','NorthWest')
I really dont get it, there is no difference. Why can i leave p2 undeclared in the test script, but not in the other script?
0 Commenti
Risposte (1)
  Walter Roberson
      
      
 il 18 Ago 2016
        In your test script, x==1 is false so the else is executed, causing p2 to be defined.
If you later change x to be 1, do you also remember to clear p2 so that it becomes undefined?
2 Commenti
  Walter Roberson
      
      
 il 19 Ago 2016
				As each item is plotted, append its handle to a vector of handles, and append its legend entry to a cell array of strings. At the end, legend() those together.
Questa domanda è chiusa.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!