How to :Data cursor displaying colorbar value?

11 views (last 30 days)
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 !

Accepted Answer

Walter Roberson
Walter Roberson on 6 May 2017
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 Comments
Dean Kueh
Dean Kueh on 8 May 2017
Edited: Dean Kueh on 8 May 2017
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 on 8 May 2017
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!