【问题标题】:Error adding legend to plot将图例添加到绘图时出错
【发布时间】:2015-10-02 11:20:01
【问题描述】:

我想在我的情节中添加一个图例。到目前为止一切都很好!它一直有效,但现在我面临一个错误。

我的绘图功能代码如下:

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 不接受二维单元格或字符串矩阵。您需要有一个 1xN 矩阵。这里的预期行为是什么?你想让第一个图例是Bus 1 VBN Voltage Angle吗?另外,你能发布一些数据来运行它吗?
  • 你说它一直有效。它在这种情况下有效吗?如果是这样,您能否发布最后一个有效的代码以及您希望图例显示的内容?
  • 嗨,好吧!不知道我需要一个 1xN 矩阵...@Ander Biguri:是的,这将是我的第一个传奇条目!在这里,我将发布一个与上面代码类似的示例:
  • 错误本身抱怨使用&&||,尝试将它们设置为&|即单个实例。
  • 我没有在代码中使用你上面看到的那些逻辑运算符!我无法向我解释!

标签: matlab plot format legend figure


【解决方案1】:

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

words={};

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

legend(words);    

【讨论】:

    猜你喜欢
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多