0001 function linelabel(vectors);
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 if(ischar(vectors) & strcmp(vectors, 'reset'))
0024 delete(findobj(gca, 'Type', 'Text'));
0025 set(findobj(gca, 'Type', 'Line'), 'LineWidth', 1);
0026 return;
0027 end
0028
0029
0030 lines = findobj(gca, 'Type', 'Line');
0031 if (isempty(lines)), error('The plot does not contain any line objects.'); end
0032
0033 ydatalines = get(lines, 'YData');
0034 xdatalines = get(lines, 'XData');
0035 datacolors = get(lines, 'Color');
0036
0037 L = unique(cellfun('length', ydatalines));
0038 if (length(L) > 1)
0039 error('LINELABEL requires all line objects in the current plot to have the same length.');
0040 end
0041 P = size(ydatalines, 1); [M,N] = size(vectors);
0042 xdatalines = cat(1, xdatalines{:});
0043 ydatalines = cat(1, ydatalines{:});
0044 datacolors = cat(1, datacolors{:});
0045
0046 if (N ~= L)
0047 error('The input matrix must have the same number of columns as the lines in the current plot.');
0048 elseif (M > P)
0049 error('The input matrix can not have more rows than the number of lines in the current plot.');
0050 else
0051 X = unique(xdatalines, 'rows');
0052 if (size(X, 1) > 1)
0053 error('LINELABEL requires all line objects in the current plot to share the same XData');
0054 end
0055 end
0056 if ((xdatalines(1) ~= 1) | (~all(all(diff(xdatalines, 1, 2) == 1))))
0057 warning(['This function is currently designed to work with XData that ' ...
0058 'starts at 1 and is evenly spaced. Behavior with current plot may be unexpected.']);
0059 end
0060
0061
0062 xlim = get(gca, 'XLim'); ylim = get(gca, 'YLim');
0063 if (M == P)
0064 while (true)
0065 [x,y,key] = ginput(1);
0066 if (isempty(x) | isequal(key, 27)), break; end
0067 nearestX = round(x);
0068 [howgood, index] = min(abs(ydatalines(:, nearestX) - y));
0069 if ((abs([(nearestX - x)./(xlim(2)-xlim(1))]) > 0.005) | ...
0070 (abs([( howgood )./(ylim(2)-ylim(1))]) > 0.005))
0071 continue;
0072 end
0073 [dist,ind] = min(pairdist(vectors, ydatalines(index,:),'nosqrt'), [], 1);
0074 label = num2str(ind);
0075 text(x, y, label, 'FontWeight', 'bold', 'FontSize', 14, 'Color', getcolor(datacolors, index));
0076 end
0077 else
0078 if (M > 100)
0079 areyousure = input(['Warning. You are trying to label > 100 lines.\n' ...
0080 'Enter y to continue or any other key to quit: '], 's');
0081 if (lower(areyousure(1)) ~= y)
0082 return;
0083 end
0084 end
0085 for (test = 1:M)
0086 index = ndsearch(ydatalines, vectors(test,:));
0087
0088
0089
0090 ydatacopy = ydatalines;
0091 ydatacopy(index,:) = Inf;
0092 dist_to_lines = abs(ydatacopy - repmat(ydatalines(index,:),[P,1]));
0093 [junk,select] = max(min(dist_to_lines, [], 1), [], 2);
0094 text(X(select)*1.01, ydatalines(index,select)*0.99, num2str(test), ...
0095 'FontWeight', 'bold', 'FontSize', 20, 'Color', getcolor(datacolors, index), ...
0096 'VerticalAlignment', 'baseline', 'HorizontalAlignment', 'right');
0097 set(lines(index), 'LineWidth', 3);
0098 uistack(lines(index), 'top');
0099 end
0100 end
0101
0102
0103
0104 function color = getcolor(datacolors, index)
0105 if (size(unique(datacolors, 'rows'), 1) > 1)
0106 color = brighten(datacolors(index,:), 0.5);
0107 else
0108 color = [1 1 1] - get(gca, 'Color');
0109 end