How to interpret ButtonDownFcn event data for axis which limit is datetime?

This seems like something obvious but I've searched for this all afternoon and can't come up with anything that addresses my issue. I've been struggling with my buttondownfcn callback on an axes which xaxis is defined in datetime.
K>> ax.XAxis
ans =
DatetimeRuler with properties:
Limits: [Aug 21, 2017, 21:00 Aug 21, 2017, 22:45]
TickValues: [Aug 21, 2017, 21:00 Aug 21, 2017, 21:15 Aug 21, 2017, 21:30 Aug 21, 2017, 21:45 Aug 21, 2017, 22:00 Aug 21, 2017, 22:15 Aug 21, 2017, 22:30 Aug 21, 2017, 22:45]
TickLabelFormat: 'HH:mm'
Show all properties
The eventinformation comes in as
K>> eventdata
eventdata =
Hit with properties:
Button: 1
IntersectionPoint: [0.927083313465118 5.139902343750000e+02 -1]
Source: [1×1 Axes]
EventName: 'Hit'
I've read that is the "location of last button click, in axes data units", but the x value doesn't look like a datenum. I thought it might be an offset from the axis limit but
K>> ax.XLim(1)+eventdata.IntersectionPoint(1)
ans =
datetime
Aug 22, 2017, 19:14
is not where I picked. I picked as closed to the 22:15 tick mark (in the attached image) as I could. It's close but there's an origin somewhere that I seem to be missing.
Can anyone shed some light on this?

6 Comments

As an experiment, try
ax.XLim(1) + diff(ax.XLim) * eventdata.IntersectionPoint(1)
Sadly, no, that didn't work
On the left side which should be 21:00, the intersection point is 0.875
in mouseButtonPressed
248 disp(['pick: ', datestr(ax.XLim(1) + diff(ax.XLim) * eventdata.IntersectionPoint(1))])
K>> eventdata.IntersectionPoint(1)
ans =
0.875000000000000
pick: 21-Aug-2017 22:31:52
On the rightside which should be 22:45, the intersection point is 0.9479.
in mouseButtonPressed
248 disp(['pick: ', datestr(ax.XLim(1) + diff(ax.XLim) * eventdata.IntersectionPoint(1))])
K>> eventdata.IntersectionPoint(1)
ans =
0.947916686534882
pick: 21-Aug-2017 22:39:31
Hi Scott,
Could you give these functions a try and see if they suit your needs. Looking at your question, num2ruler is a more likely candidate. You'll need to pass it the specific ruler you are working with:
Campion, thank you. At first blush, that seems to be exactly what I was looking for. Specifically, for my use case, to convert a button down event on the axes to the appropriate timedate variable, I used:
function mouseButtonPressed(this, object, eventdata, ax)
disp('in mouseButtonPressed')
eventdata.IntersectionPoint
disp(['pick: ', datestr(num2ruler(eventdata.IntersectionPoint(1), ax.XAxis))])
end
I realize that in this instance the inclusion of 'ax' is redundant as it would be the 'object' as well.
You're welcome Scott. Hope it does solve your needs at the end.
On a side note, since you have a datetime coming out of num2ruler, you can directly convert it to text using char(...) or, if you have 16b or later, string(...)
Campion Loong, I suggest you copy your responses to be an Answer so they can be Accepted.

Sign in to comment.

 Accepted Answer

As Walter suggested, pasting my comment as answer:
Give these functions a try and see if they suit your needs. Looking at your question, num2ruler is a more likely candidate. You'll need to pass it the specific ruler you are working with:

2 Comments

function mouseButtonPressed(this, object, eventdata, ax)
disp('in mouseButtonPressed')
eventdata.IntersectionPoint
disp(['pick: ', datestr(num2ruler(eventdata.IntersectionPoint(1), ax.XAxis))])
end
Thanks! This was not trivial once I zoomed in or panned the figure. Should be in Matlab documentation itself for the ButtonDownFcn. But this works exactly as intended.

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!