getpts - can't select multiple points when plot contains datetime axis

11 views (last 30 days)
Time = [datetime('now'):seconds(1):datetime('now')+minutes(1)];
Value = randn(61,1);
plot(Time, Value);
[TimePoints, ~] = getpts;
When I select one point (left-click) and press return, everything is fine. But when I try to select multiple points I get this error message:
Error using datetime/horzcat (line 1292)
All inputs must be datetimes or date/time character vectors or date/time
strings.
Error in getpts>NextButtonDown (line 250)
set([GETPTS_H1 GETPTS_H2], 'XData', [x newx], ...
Error in getpts (line 36)
feval(varargin{:});
Error using getpts (line 108)
Error while evaluating Figure WindowButtonDownFcn.
The getpts documentation says that the output argument is a double. I presume that is the issue. But it will return one datetime value. So I'm hoping there's a way to get it to return more. Is that possible? Or do I have to convert the datetime values to datenum values, make the plot, use getpts and then convert them back again? Thanks.
  3 Comments
Adam Reynolds
Adam Reynolds on 11 Aug 2018
Sorry, not sure what you mean. "getpts" is a function in the image processing toolbox.
which -all getpts
/Applications/MATLAB_R2018a.app/toolbox/images/images/getpts.m
dpb
dpb on 11 Aug 2018
Edited: dpb on 11 Aug 2018
Hmmm...I don't have the TB and the TMW search engine didn't locate it and it wasn't at FEX so I didn't know.
This time it did locate...not sure what's up w/ that.
As you say, the doc clearly indicates it expects only a double as its return; it hasn't been updated to handle the overloaded plot functions.
You've got the option you outlined which would be somewhat painful or, since it's an m-file you could look into its handling of returned values and see how complex it might be to add the facility to handle datetime return values; since I don't have TB, can't see the code here to tell.
I don't know how feasible it would be to install a callback routine to trap the returned coordinate to do the conversion; you probably will have to make changes in the function itself but might see if they left a hook available.
Definitely worth at least an enhancement request if not bug report...
ADDENDUM TMW may think since it's in Image Processing TB that images don't have datetime axes...
ginput does return an array of coordinates but I've not yet exactly figured out the x-axes scaling when in datetime coordinates...

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 11 Aug 2018
getpts() is meant to work with images and return x,y values. The two axes are supposed to have the same units, which yours don't, and which is explained by the error message "All inputs must be datetimes or date/time character vectors or date/time strings."
I suggest you use ginput() instead.
  3 Comments
Adam Reynolds
Adam Reynolds on 11 Aug 2018
Edited: Adam Reynolds on 11 Aug 2018
I wasn't aware of ginput(). I prefer the way getpts() works since it leaves markers where you have clicked, but ginput() is acceptable. The ginput() output from a datetime axis is not in datetime format. But as specified here, it can be converted using the specific axis ruler by the num2ruler function. This is what I am using:
Time = [datetime('now'):seconds(1):datetime('now')+minutes(1)];
Value = randn(61,1);
plot(Time, Value);
ax = gca;
[x,y] = ginput;
xdate = num2ruler(x,ax.XAxis);
Thanks you both for your help. I really appreciate it. Ideally, I would use getpts(), but I think editing the function to do what I want, if even possible, would take me too much time. I understand that getpts() was designed as part of the image processing toolbox, but I'm sure plenty of people use it on non-image figures and accommodating axes with different units would be a useful feature I think.
dpb
dpb on 11 Aug 2018
Ah! I wasn't aware of num2ruler...name proliferation will eventually sink the ship I'm afraid, it's just not possible to keep track of all the new stuff that keeps showing up...it's hard enough when one has 30 yrs previous experience with the base product and a few TBs; can't imagine how overwhelming it must be to the new user...

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!