MATLAB: Ignoring markers and lines in legend

legendmarkerplot

I want to ignore markers in my legend but do not know how to do this properly. k in this code is the variable in the for loop that is being incremented.
C = {'k','b','r','g','y',[.5 .6 .7],[.8 .2 .6]};
Plot1 = loglog(Discret,L2err,'.r', 'MarkerSize',15);
hold on
Plot2 = loglog(Discret,L2err,'color',C{k}, 'LineWidth', 2);
legendInfo{k} = ['Factor = ' num2str(k)];
legend(Plot2,legendInfo)
legend(legendInfo)
triang_x = [Discret(2), Discret(5)];
triang_y = interp1(Discret, L2err, triang_x);
hold on
z = loglog(triang_x([1,2,2]), triang_y([1,1,2]), 'k', 'LineWidth', 2);
grid on
Problem is also that the lines of the triangle also goes into the legend. Any ideas on how to fix this? Really appreciate it!

Best Answer

...
legendInfo{k} = ['Factor = ' num2str(k)];
legend(Plot2,legendInfo)
legend(legendInfo)
...
If you want only certain lines to be in the legend, then use the first form but with the subset array of only the line handles (and labels to match) in the call to legend.
figure
hold on
...
legendInfo={num2str([1:N].','Factor = %d')};
for k=1:N
...
hL1(k) = loglog(Discret,L2err,'.r', 'MarkerSize',15);
hL2(k) = loglog(Discret,L2err,'color',C{k}, 'LineWidth', 2);
triang_x = [Discret(2), Discret(5)];
triang_y = interp1(Discret, L2err, triang_x);
hLz = loglog(triang_x([1,2,2]), triang_y([1,1,2]), 'k', 'LineWidth', 2);
end
grid on
legend(hL2,legendInfo)
should come close if I interpret what you want correctly.
ADDENDUM
To emulate your loop with fake data but to only label the lines...salt to suit.
% dummy data to emulate lines on plot
x=[1:10];
y=-0.1*x+2; y=repmat(y,5,1)+linspace(0,-0.8,5).';
x=x.'; y=y.';
nc=size(y,2);
C = {'k','b','r','g','y',[.5 .6 .7],[.8 .2 .6]};
legendInfo=num2str([1:nc].','Factor = %d');
hL=zeros(nc,1);
figure, hold on
for k=1:nc
hL(k)= plot(x,y(:,k), 'color',C{k}, 'LineWidth', 2, ...
'markeredgecolor','r', 'markerfacecolor','r', ...
'marker','.', 'MarkerSize',15);
tx=[x(2), x(5)];
ty=interp1(x,y(:,k),tx);
plot(tx([1,2,2]), ty([1,1,2]), 'k', 'LineWidth',0.5);
end
legend(hL,legendInfo)
grid on
The chosen data makes the triangles overlap but the effect is what is shown in your figure and note there are no labels excepting for the lines...I did combine the characteristics into the one line object so it does reflect the marker as well as color; if you don't want that effect, then you can revert to the two lines but again, just pass the one set of handles to legend and those are all you'll get.
Also notice there's a fair amount of stuff that can be moved out of the loop and vectorized and done before/afterwards to simplify the loop itself...
ADDENDUM 2
tx=[x(2), x(5)];
ty=interp1(x,y(:,k),tx);
plot(tx([1,2,2]), ty([1,1,2]), 'k', 'LineWidth',0.5);
If the points are going to be existing ones, there's no need for interp1 here;, just build ty the same was as tx. Perhaps began or will be going to use other points???
i1=2; i2=5; % Set the desired points; could be variable...
itx=[i1 i2 i2]; % Build the addressing vector from them
plot(x(itx), y(itx), 'k', 'LineWidth',0.5); % and use the indexing vector