How to :Data cursor displaying colorbar value?

조회 수: 13 (최근 30일)
Dean Kueh
Dean Kueh 2017년 5월 6일
댓글: Walter Roberson 2017년 5월 8일
Hi guys,
Currently I have a 2D scatter plot with 3 column vectors. They are Pj, ecc and Combo.bresult .The third column is represented by a color bar.
I am lost as to how to display the third column value(the color bar value) when using data cursor mode. Currrently it only shows the x and y coordinate.
Thanks !

채택된 답변

Walter Roberson
Walter Roberson 2017년 5월 6일
dcm_obj = datacursormode();
set(dcm_obj,'UpdateFcn',@(hObject, event_obj) myupdatefcn(hObject, event_obj, Pj, ecc, Combo.bresult) );
function output_txt = myfunction(~, event_obj, Pj, ecc, bresult)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text
%find the closest data point to the cursor
cursor_pos = event_obj.Position;
dist2_to_dots = (Pj - cursor_pos(1)).^2 + (ecc - cursor_pos(2)).^2;
[~, item_idx] = min(dist2_to_dots);
output_txt = {sprintf('Pj: %g', Pj(item_idx)), sprintf('ecc: %g', ecc(item_idx)), sprintf('br: %g', bresult(item_idx)) };
  댓글 수: 10
Dean Kueh
Dean Kueh 2017년 5월 8일
편집: Dean Kueh 2017년 5월 8일
omg it worked!
Do I have to edit the function file for every different graph that I plot? I have another 4 of the same plots but different color bar columns
That is different columns within the Combo.bresult vector matrix.
Walter Roberson
Walter Roberson 2017년 5월 8일
You can pass the text formats to use into mypdatefcn to generalize it
set(dcm_obj, 'UpdateFcn', @(hObject, event_obj) myupdatefcn(hObject, event_obj, X, Y, Z, {'ecc: %g', '1/Pj: %g', 'br: %g'}) );
and
function output_txt = myupdatefcn(~, event_obj, X, Y, Z, formats)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text
%find the closest data point to the cursor
cursor_pos = event_obj.Position;
dist2_to_dots = (X - cursor_pos(1)).^2 + (Y - cursor_pos(2)).^2;
[~, item_idx] = min(dist2_to_dots);
output_txt = {sprintf(formats{1}, X(item_idx)), formats{2}, Y(item_idx)), sprintf(formats{3}, Z(item_idx)) };
However, note that you can only have one active dcm object per figure, so if you want to be able to point to different lines and have different text labels come up depending on what was being pointed to, then you need to code a single update function that handles everything simultaneously.
One approach to handle multiple plots simultaneously would be to set the UserData property of each drawn object to have the associated extra data and the text formats to use; then the data cursor update function would have to figure out which object it was near, and retrieve the appropriate information to update the display. See related https://www.mathworks.com/matlabcentral/answers/219797-check-if-mouse-is-above-an-axes#answer_180771

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by