MATLAB Answers

How can I view the formatted date string(DATESTR) while using Date Cursor for a plot containing date strings on MATLAB 8.1(R2013a)?

20 views (last 30 days)
When I plot a date with some data, it seems I have to pass in DATENUM instead of DATESTR to the plot function. Thus, when I click on the data point, I see a DATENUM value, not a straight forward DATESTR that I can understand. Is there a way in which I can display the readable DATESTR format?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Oct 2013
A simple way to achieve this is to change the display format in the UpdateFcn of the Data Cursor.
Consider the following command which plots the date value on the x-axis:
plot(datenum(dateVal), yValue);
Create a Data Cursor object as follows:
dcm_obj = datacursormode(gcf);
You need to assign a callback function to UpdateFcn. Let's call this MYFUNCTION. MYFUNCTION specifies how the data cursor tooltip should display the coordinate values.
set(dcm_obj, 'UpdateFcn',@myfunction);
In MYFUNCTION specifu the display format as DATESTR for the appropriate coordinate(in this case, x-coordinate.)
function output_txt = myfunction(obj,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
output_txt = {['X: ', datestr(pos(1))],... % Notice the X coordinate value has been changed to DATESTR
['Y: ',num2str(pos(2),4)]};
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ',num2str(pos(3),4)];


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!