0
legend(errorbars, {'5-15 Percent Exiting Agents', '25-35 Percent Exiting Agents','55-65 Percent Exiting Agents','x','y','z'},'Location','NorthWest');

给出错误:

使用图例时出错(第 120 行) 参数无效。键入“帮助图例”以获取更多信息。

>> errorbars

errorbars =

    0.0011    0.0012    1.0012    2.0012    3.0012    4.0012

我不知道为什么会发生此错误。因为它有六个错误栏的 6 个标签!

另外,发布完整代码:

for k=1:2,    %x-axis variables
    for i=1:size(StringDes,2), %y-axis outputs
        errorbar_index=0;
        set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
        for j=1:3,  % two loops for 6 errorbars
            for z=1:2,
                errorbar_index=errorbar_index+1;
                filter2= (pe{z}<upp(j) & pe{z} >lowp(j));
                dataset=Descriptors{z}{i};
                var= Variables{z}(:,k);
                y=dataset(filter2);
                x=var(filter2);
                if(k==2)
                    range=rangeValues_ks;
                else
                    range=rangeValues_ke;
                end
                filter3=(ismember(x,range)) ;
                [vals, ~, id] = unique(x(filter3));
                m = accumarray(id, y(filter3), [], @mean);
                s = accumarray(id, y(filter3), [], @std);
                errorbars(errorbar_index)=errorbar(vals,m,s,colors(j), 'LineStyle',linestyle{z},'Marker',marker{z},'LineWidth',linewidth(z));
            end            
            xlabel(StringVar(k));
            ylabel(StringDes(i));
            hold all;            
        end
        filename=char(strcat(StringDesFileName(i),StringVarFileName(k)));
        legend(errorbars, {'5-15 Percent Exiting Agents', '25-35 Percent Exiting Agents','55-65 Percent Exiting Agents','x','y','z'},'Location','NorthWest');
        title('');
        print(gcf,strcat(parent_folder,'/',filename),'-dpng','-r375' );
        close gcf;
    end
end
4

1 回答 1

1

您不能将图例与ErrorBar数组(此处为1x6数组)中的多个条目相关联。您可以做的是将该图例与ErrorBar创建对象的轴相关联。它们确实Children是轴,因此如果您愿意,可以将图例与它们Parent(即轴)相关联。

因此,您可以将有问题的行更改为:

legend(gca, {'5-15 Percent Exiting Agents', '25-35 Percent Exiting Agents','55-65 Percent Exiting Agents','x','y','z'},'Location','NorthWest');

或这个:

legend(get(errorbars(1),'Parent'), {'5-15 Percent Exiting Agents', '25-35 Percent Exiting Agents','55-65 Percent Exiting Agents','x','y','z'},'Location','NorthWest');

请注意,如果您的工作会话中只有 1 个活动轴,那么您可以gca在第一种情况下完全省略。

于 2015-09-08T13:27:32.407 回答