1
我想爲我的圖添加圖例。到目前爲止非常好!它一直工作,但現在我面臨一個錯誤。將圖例添加到圖的錯誤
我的繪圖功能的代碼如下:
function [ ] = plot_mti_IV(Bus_indizes, Bus_voltages,new_results, names)
global Timeslot
m={'-','--',':','-','--',':','-.','-*','-^','-.','-*','-^'};
Timeslot_temp=[1:1:Timeslot(end)];
name_index=find(Bus_voltages==1); %Find index of depicted Variable
%Initialise legend
words=['Bus' num2str(Bus_indizes(1)) names{name_index(1)}]
words=[words ;['Bus' num2str(Bus_indizes(1)) names{name_index(1)}]]
num_Bus=length(Bus_indizes);
colors = distinguishable_colors(num_Bus); %distinguishable_colors.m: Function from Mathworks File Exchange. See license for Copyright!
for i=1:length(name_index) %for number of chosen variables
for j=1:num_Bus %plot this value for all chosen busses
if j==1
plot_data=new_results{i,1}(Timeslot_temp,Bus_indizes(j)); %collect data to be plotted in array
plot(plot_data,m{i},'Color',colors(j,:));
hold on
xlim([Timeslot(1) (Timeslot(end)+Timeslot(end)*0.05)]); %Adjustment, so that legend does not cover graph
hold on
xlabel('Time');
ylabel('Voltage');
else
words= [words ; ['Bus' num2str(Bus_indizes(j)) names{name_index(i)} ]];
plot_data=new_results{i,1}(Timeslot_temp,Bus_indizes(j)); %collect data to be plotted in array
plot(plot_data,m{i},'Color',colors(j,:));
hold on
end
end
end
legend(words); %Add legend to graph
end
我得到一個錯誤
Operands to the || and && operators must be convertible to logical scalar values.
Error in legend (line 194)
elseif narg > 0 && ~ischar(varargin{1}) && ...
當我執行legend(words)
。
表達legend(words)
之前,words
的格式是:
words =
'Bus' '1' 'VBN Voltage Angle'
'Bus' '4' 'VBN Voltage Angle'
'Bus' '2' 'VBN Voltage Angle'
'Bus' '2' 'VCN Voltage Angle'
我無法理解這個錯誤,是對你的幫助非常高興!
嗨,啊,好吧!不知道我需要一個1xN的矩陣...... @ Ander Biguri:是的,這應該是我的第一個傳奇入口!在這裏,我將張貼的行爲就像上面的代碼示例:
[ output_args ] = test_plot()
figure
Timeslot=[2:1:7]
Bus_indizes=[1,2,3,5]
Bus_voltages=[0,0,0,0,1,1,0,0,0,0,0,0]
new_results=magic(12)
names=cell(1,12);
names{1,1}={'VAN Voltage Magnitude'};
names{1,2}={'VBN Voltage Magnitude'};
names{1,3}={'VCN Voltage Magnitude'};
names{1,4}={'VAN Voltage Angle'};
names{1,5}={'VBN Voltage Angle'};
names{1,6}={'VCN Voltage Angle'};
names{1,7}={'V1 Voltage Magnitude'};
names{1,8}={'V2 Voltage Magnitude'};
names{1,9}={'V0 Voltage Magnitude'};
names{1,10}={'V1 Voltage Angle'};
names{1,11}={'V2 Voltage Angle'};
names{1,12}={'V0 Voltage Angle'};
m={'-','--',':','-','--',':','-.','-*','-^','-.','-*','-^'};
Timeslot_temp=[1:1:Timeslot(end)]; %Array containing all time instants from absolute Beginning of simulation (not necessarily the first value of the timeslot) till the end of the timeslot
name_index=find(Bus_voltages==1); %Find index of depicted Variable
%Initialise words
words=['Bus' num2str(Bus_indizes(1)) names{name_index(1)}]
words=[words ;['Bus' num2str(Bus_indizes(1)) names{name_index(1)}]]
num_Bus=length(Bus_indizes);
colors = distinguishable_colors(num_Bus); %distinguishable_colors.m: Function from Mathworks File Exchange. See license for Copyright!
for i=1:length(name_index) %for number of chosen variables
for j=1:num_Bus %plot this value for all chosen busses, color is changed for every bus
if j==1
plot_data=new_results(Timeslot_temp,Bus_indizes(j)); %collect data to be plotted in array
plot(plot_data,m{i},'Color',colors(j,:));
hold on
xlim([Timeslot(1) (Timeslot(end)+Timeslot(end)*0.05)]); %Adjustment, so that legend does not cover graph
hold on
xlabel('Time');
ylabel('Voltage'); %Label y axis with name of the chosen variable
else
words= [words ; ['Bus' num2str(Bus_indizes(j)) names{name_index(i)} ]];
plot_data=new_results(Timeslot_temp,Bus_indizes(j)); %collect data to be plotted in array
plot(plot_data,m{i},'Color',colors(j,:));
hold on
end
end
end
legend(words); %Add legend to graph
end
'legend'不接受2D細胞或字符串矩陣。你需要有一個1xN矩陣。這裏預期的行爲是什麼?你希望第一個圖例是'Bus 1 VBN Voltage Angle'嗎?另外,你可以發佈一些數據來運行它嗎? –
你說它一直工作。它在這方面起作用了嗎?如果是這樣,你可以發佈最後的代碼工作,你想要傳說顯示? – mabe
嗨,啊,好吧!不知道我需要一個1xN的矩陣...... @ Ander Biguri:是的,這應該是我的第一個傳奇入口!在這裏,我將發佈一個與上面的代碼行爲相似的示例: – john