MATLAB: How to customize the data cursor like it is made in BAR function with STACK option in MATLAB 7.5 (R2007b)

MATLAB

The following code
fig = figure;
z = peaks;
z=z(20:25,40:43);
bar(z,'stack');
produces a plot in which the data cursor displays three values: X, Y (Stacked), Y (Segment).
If I create a 'UpdateFcn' for the datacursormode object then I only have access to two values through the 'Position' property: X and Y (Stacked). This is illustrated by the following code:
function doc_datacursormode
fig = figure;
z = peaks;
z=z(20:25,40:43);
bar(z,'stack');
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',@myupdatefcn);
function txt = myupdatefcn(empt,event_obj)
pos = get(event_obj,'Position');
txt = {['X: ',num2str(pos(1))],...
['Y (Stacked): ',num2str(pos(2))]};
I would also like to have access to the Y (Segment) value, which the default data cursors show.

Best Answer

In order to get the Segment value, you can use the YDATA property of the current object (that is, the graphic object that was clicked).
Here is an example:
function doc_datacursormode
fig = figure;
z = peaks;
z=z(20:25,40:43);
bar(z,'stack');
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',@myupdatefcn);
function txt = myupdatefcn(empt,event_obj)
pos = get(event_obj,'Position');
val = get(gco,'YData');
txt = {['X: ',num2str(pos(1))],...
['Y (Stacked): ' num2str(pos(2))], ...
['Y (Segment): ',num2str(val(pos(1)))]};
In this example:
- pos (1) is the X value
- pos (2) is the Y (Stacked) value
- val(pos(1)) is the Y (Segment) value