Data Cursor Position Issue

5 views (last 30 days)
Conor
Conor on 20 Jun 2011
I have a number of bar charts in a figure window and each has the datacursormode enabled. When I click on a bar with a negative value, a y position of 0 is returned. It seems to work fine for bars of positive values so I'm somewhat perplexed.
  2 Comments
Richard
Richard on 20 Jun 2011
A simple example ( bar(-5:5) ) works OK for me. Do you have some code that reproduces the problem? Also, which version are you using?
Conor
Conor on 28 Jun 2011
Im using 7.4.0
main program goes through a complex set of calculations to determine values to plot and calls the function below.
function BarPlots(Radii, RDI,...)
global H; %H is a global variable containing handles to multiple uicontrols and plots in a gui
...
H.hRDI=subplot('position',[3/6 b+h/2.5 w h/1.575]);% b w h]);
bar(Radii,RDI)
% After main progam has run, using the Data Cursor tool and clicking on a
% plot triggers this callback function
function CT = DataCursorClick(empty,eventdata)
global H;
set(H.hContourPlot,'fill','on')
P = get(eventdata,'Position');
T = get(eventdata,'Target');
G = get(T,'Parent');
if % structure to determine which axes has been clicked on
...
elseif G == H.hRDI
CT = {['Radius: ',num2str(P(1))],['RDI: ',num2str(P(2))]};
elseif
...
end
The problem seems to be coming from
P = get(eventdata,'Position');
When the y value, P(2) is negative on the bar plot, 0 is returned for P(2).
Thanks for the help!!

Sign in to comment.

Accepted Answer

Richard
Richard on 3 Aug 2011
Conor, sorry for the delay, I didn't notice your comment until today.
This is a bug in older versions of MATLAB. I can see the issue in v7.4 for a simple bar chart with a standard data cursor:
bar(-5:5)
For all of the bars, the data cursor is being placed at the maximum Y value instead of at the maximum absolute Y value, and for negative bars this is always 0 instead of being -5, -4, -3 etc. This incorrect value is also passed into your custom datatip function. There is no easy way to directly get the correct Y (RDI) value, however you could look up the X (Radii) value in the target's XData and then use the corresponding YData value:
xd = get(T, 'XData');
[~,idx] = min(abs(xd-P(1)));
yd = get(T, 'YData');
ThisRDI = yd(idx);
This bug has been fixed in a more recent version of MATLAB - I believe from R2008b onwards, so upgrading is another option.
  1 Comment
Conor
Conor on 4 Aug 2011
Thanks Richard
I did end up using the XData and YData of the plots to display the correct value. Only issue with that is the actual data cursor remians at the 0 location for negative bars. I haven't played with it in a few weeks now, but I'll try changing the data cursors position in the callback next.
Thanks again!

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!