将图例添加到绘图时出错

时间:2015-10-02 11:20:01

标签: matlab plot format legend figure

我想在我的情节中添加一个传奇。到目前为止真好!它一直有效,但现在我面临一个错误。

我的绘图功能代码如下:

=LEFT( V342;FIND( "/"; V342 ) - 1 ) / RIGHT( V342; LEN( V342) - FIND( "/"; V342 ) )

我收到错误

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

我无法理解这个错误,非常高兴你的帮助!

嗨,好吧!不知道我需要一个1xN矩阵...... @ Ander Biguri:是的,这将是我的第一个传奇入口!在这里,我将发布一个与上面代码相​​似的示例:

words = 

    'Bus'    '1'    'VBN Voltage Angle'
    'Bus'    '4'    'VBN Voltage Angle'
    'Bus'    '2'    'VBN Voltage Angle'
    'Bus'    '2'    'VCN Voltage Angle'

1 个答案:

答案 0 :(得分:0)

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

words={};

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

legend(words);