Info

此问题已关闭。 请重新打开它进行编辑或回答。

Issues with the legend of a plot with hidable lines.

1 次查看(过去 30 天)
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