Get plot lines title in a GUI plot by mouse click on each line
조회 수: 8 (최근 30일)
이전 댓글 표시
Hi! I am trying to incorporate a function or button in a GUI via GUIDE, so as to show me the tilte of each plotted line in a plot.
Namely, I am plotting various spectra (i.e. x,y data) and I would like to click on each line and get the title of the spectrum (see attached a picture)!
Could anyone provide me with a hint on how to do this?
Thank you in advance!
댓글 수: 0
채택된 답변
Jan
2022년 1월 6일
편집: Jan
2022년 1월 6일
You can define a ButtonDownFcn for the lines:
LineH(1) = plot(1:10, rand(1, 10), 'r', 'DisplayName', 'Line 1');
LineH(2) = plot(1:10, rand(1, 10), 'b', 'DisplayName', 'Line 2');
TitleH = text(0.01, 0.01, '', 'Units', 'normalized', ...
'VerticalAlignment', 'top');
LineH.ButtonDownFcn = {@showTitle, LineH, TitleH}; % [EDITED] Typo: "," -> "="
function showTitle(aLineH, EventData, LineH, TitleH)
TitleH.String = aLineH.DisplayName;
LineH.LineWidth = 0.5;
aLineH.LineWidth = 1.5;
uistack(aLineH, 'top');
end
댓글 수: 3
Jan
2022년 1월 6일
편집: Jan
2022년 1월 6일
My code does not contain a loop.
How are the names defined, you want to show as titles? If they are a cell string:
X = 1:10;
Y = rand(2, 10);
Name = {'Line 1', 'Line 2'};
LineH = plot(X, Y);
% Set LineH(1).DisplayName to Name{1},
% LineH(2).DisplayName to Name{2} and so on:
set(LineH, {'DisplayName'}, Name(:));
TitleH = text(0.01, 0.01, '', 'Units', 'normalized', ...
'VerticalAlignment', 'top');
LineH.ButtonDownFcn = {@showTitle, LineH, TitleH};
function showTitle(aLineH, EventData, LineH, TitleH)
TitleH.String = aLineH.DisplayName;
LineH.LineWidth = 0.5;
aLineH.LineWidth = 1.5;
uistack(aLineH, 'top');
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Title에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!