0

I have to plot a group of lines (10 in total, divided in 2 groups of 5 each), but I want the legend only for the groups.

It seems easy, I followed the accepted answer here: legend for group of lines

and it makes sense to me, but when I execute it, I get an error:

Operands to the || and && operators must be convertible to logical scalar values. Error in legend (line 198) elseif narg > 0 && ~ischar(varargin{1}) && ...

this is the code, I don't know if it's because of the "eval" command but I get the plot...

figure(3)
h1=plot(res(:,17),(res(:,16)./10^6),prop,...
res(:,64),(res(:,92)./10^6),prop,...
res(:,65),(res(:,93)./10^6),prop,...
res(:,66),(res(:,94)./10^6),prop,...
res(:,67),(res(:,95)./10^6),prop,...
'linewidth',2);
hold on
for ii=1:ngrains
      eval(['h2(',num2str(ii),',1)=plot(',names{ii},'.VMSTRAIN,',names{ii},'.VMSTRESS,prop2,''linewidth'',2)']);
hold on

plot

Here is the code I used to add the legend:

   legend([h1 h2],{'label1', 'label2'});
Community
  • 1
  • 1
  • 1
    Ugh, do not use `eval` like this. Store your data in a [structure](http://www.mathworks.com/help/matlab/structures.html) and utilize [dynamic field references](http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/). Or use a [map container](http://www.mathworks.com/help/matlab/map-containers.html) – sco1 Dec 14 '15 at 12:26

3 Answers3

3

There is no way of declaring a group of LineSeries via the plot function. What you can do is to "fake" the grouping, by concatenating the values and inserting NaNs between them for the purpose of breaking the lines:

%// Build some data
t  = transpose(0:0.001:pi);
x1 = sin(t);
x2 = sin(t + pi/6);
y1 = cos(t);
y2 = cos(t + pi/6);

%// Aggregate data for plotting
TX = [ t; NaN;  t];
 X = [x1; NaN; x2];
TY = [ t; NaN;  t];
 Y = [y1; NaN; y2];

%// Do the plotting
plot(   TX, X, '--b',  TY, Y, '-r');
legend('dotted blue',   'full red');

Please note that you cannot handle separately the visibility (or other property values) of the LineSeries, because there are no actual separated LineSeries within a "group".

Later Edit

If you want to use the trick of annotating only the first LineSeries from the group that share the same styles, maybe you should write:

 legend([h1(1), h2(1)], 'string1', 'string2');
  • Didn't see your post when I made mine. I feel that it is better to make legend entries for selected line series than to mess with the data. Nevertheless, nice NaN trick. – Mad Physicist Dec 14 '15 at 12:53
  • 1
    @MadPhysicist I agree that annotating only some representative `LineSeries` is more clean and flexible. The only problem (which I personally face while exchanging data trough `.fig` files -- believe me it happens :-D) is reconstructing the groups from the `LineSeries` properties like `Color`, `LineStyle`, `LineWidth` etc. In contrast it is quite easy to get the data for the entire group, and split it around the `NaN`s. –  Dec 14 '15 at 14:03
0

The result of plot is a vector that contains one entry for every line series plotted. In your case h1 is 5 x 1, and h2 is ngrains x 1. You should do legend([h1(1) h2(1)], {'label1', 'label2'});. This will assign a legend entry only to the first line series of each group.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
-1

It seems that your h2 column vector and you are trying to concatenate with h1 as if it were a row vector.

try legend([h1;h2], ... or legend([h1, h2'],...

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
50k4
  • 241
  • 2
  • 11