Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Issues with the legend of a plot with hidable lines.

조회 수: 1 (최근 30일)
mec123
mec123 2016년 8월 18일
마감: MATLAB Answer Bot 2021년 8월 20일
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?

답변 (1개)

Walter Roberson
Walter Roberson 2016년 8월 18일
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
mec123
mec123 2016년 8월 19일
No i dint clear it, so that might be the difference... But the behaviour of my original script is more likely anyways.
Is there a way to create a legend which is fitting for my concern without a complicated if-else structure?
Walter Roberson
Walter Roberson 2016년 8월 19일
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.

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by