How to :Data cursor displaying colorbar value?

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

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

Hi there, thanks for responding. However, I am getting an error saying "Error in custom datatip string function"
I've also edited the code to suit my results
dcm_obj = datacursormode();
set(dcm_obj,'UpdateFcn',@(hObject, event_obj) myupdatefcn(hObject, event_obj, 1./Pj(1,:), ecc(1,:), Combo.bresult(:,1)) );
function output_txt = myfunction(~, event_obj, 1./Pj(1,:), ecc(1,:), Combo.bresult(:,1))
% ~ 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 = (1./Pj(1,:) - cursor_pos(1)).^2 + (ecc(1,:) - cursor_pos(2)).^2;
[~, item_idx] = min(dist2_to_dots);
output_txt = {sprintf('Pj: %g', 1./Pj(1,:)(item_idx)), sprintf('ecc: %g', ecc(1,:)(item_idx)), sprintf('br: %g', Combo.bresult(:,1)(item_idx)) };
I went to data cursor and "Select text update function"
The function definition line
function output_txt = myfunction(~, event_obj, 1./Pj(1,:), ecc(1,:), Combo.bresult(:,1))
is not valid. You need to give only variables or ~ on that line.
You have already computed 1./Pj and Combo.bresult by way of the
@(hObject, event_obj) myupdatefcn(hObject, event_obj, 1./Pj(1,:), ecc(1,:), Combo.bresult(:,1))
so receive those values as variables like the way I showed and just use the variables instead of continually recalculating.
I've tried it and it still doesn't work..
Is it because my results are not in a column 1 matrix in each variable?
There are multiple columns within Pj and ecc so I have to select the coulumn that I need.
Just some minor function name problems.
Example:
Pj = rand(1,30); ecc = rand(1,30); Combo.bresult = randi(5,1,30);
pointsize = 20;
scatter(Pj, ecc, pointsize, Combo.bresult);
colorbar;
dcm_obj = datacursormode();
set(dcm_obj,'UpdateFcn',@(hObject, event_obj) myupdatefcn(hObject, event_obj, Pj, ecc, Combo.bresult) );
function output_txt = myupdatefcn(~, 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)) };
I tried and still getting the same error..
what am i doing wrong? Here is my code on plotting the figure.
%sigma ci
figure;
scatter(ecc(1,:),1./Pj(1,:), sz, Combo.bresult(:,1),'filled')
xlabel('Eccentricity');
ylabel('1/P_{j}');
dcm_obj = datacursormode();
set(dcm_obj,'UpdateFcn',@(hObject, event_obj) myupdatefcn(hObject, event_obj, Pj, ecc, Combo.bresult) );
hold on
plot(x_ti,y_ti_plot,x_ci,y_ci_plot,x_ts,y_ts_plot,x_cs,y_cs_plot);
caxis([0 3]);
hold off
axis([-850 -100 3e-8 15e-8]);
title('Reliability index of P vs e (\sigma_{ci})');
Pj_dc = Pj(1,:) ;
ecc_dc = ecc(1,:);
Combo.bresult_dc = Combo.bresult(:,1);
sz = 20;
%sigma ci
figure;
X = ecc(1,:);
Y = 1./Pj(1,:);
Z = Combo.bresult(:,1)
scatter(X, Y, sz, Z, 'filled')
xlabel('Eccentricity');
ylabel('1/P_{j}');
dcm_obj = datacursormode();
set(dcm_obj,'UpdateFcn',@(hObject, event_obj) myupdatefcn(hObject, event_obj, X, Y, Z) );
hold on
plot(x_ti, y_ti_plot, x_ci, y_ci_plot, x_ts, y_ts_plot, x_cs, y_cs_plot);
caxis([0 3]);
hold off
axis([-850 -100 3e-8 15e-8]);
title('Reliability index of P vs e (\sigma_{ci})');
Pj_dc = Pj(1,:) ;
ecc_dc = ecc(1,:);
Combo.bresult_dc = Combo.bresult(:,1);
function output_txt = myupdatefcn(~, event_obj, X, Y, Z)
% ~ 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('ecc: %g', X(item_idx)), sprintf('1/Pj: %g', Y(item_idx)), sprintf('br: %g', Z(item_idx)) };
Thanks a bunch man, I appreciate your help but its still giving me the same error.
"Error in custom data string function"
Example, tested:
N = 50;
ecc = randn(1,N);
Pj = exp(randn(1,N));
Combo.bresult = cos(sin(ecc .* Pj * 1i)) .';
sz = 20;
%sigma ci
figure;
X = ecc(1,:);
Y = 1./Pj(1,:);
Z = Combo.bresult(:,1);
scatter(X, Y, sz, Z, 'filled')
xlabel('Eccentricity');
ylabel('1/P_{j}');
dcm_obj = datacursormode();
set(dcm_obj,'UpdateFcn',@(hObject, event_obj) myupdatefcn(hObject, event_obj, X, Y, Z) );
hold on
%{
plot(x_ti, y_ti_plot, x_ci, y_ci_plot, x_ts, y_ts_plot, x_cs, y_cs_plot);
caxis([0 3]);
%}
hold off
%{
axis([-850 -100 3e-8 15e-8]);
%}
title('Reliability index of P vs e (\sigma_{ci})');
Pj_dc = Pj(1,:) ;
ecc_dc = ecc(1,:);
Combo.bresult_dc = Combo.bresult(:,1);
function output_txt = myupdatefcn(~, event_obj, X, Y, Z)
% ~ 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('ecc: %g', X(item_idx)), sprintf('1/Pj: %g', Y(item_idx)), sprintf('br: %g', Z(item_idx)) };
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.
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)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!