将图例添加到绘图时出错

Error adding legend to plot

我想为我的情节添加一个图例。到目前为止这么好!它一直有效,但现在我遇到了一个错误。

我的绘图函数代码如下:

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:是的,这将是我的第一个传奇条目!在这里,我将 post 一个行为与上面的代码相同的示例:

[ 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

我现在使用函数 strjoin 解决了这个问题:

words={};

temp=['Bus' num2str(Bus_indizes(j)) names{variables_index(i)} ];
words{end+1}=strjoin(temp); 

legend(words);