MATLAB Answers

How to set the axis to display hours?

22 views (last 30 days)
ccs
ccs on 26 Feb 2015
Commented: ccs on 27 Feb 2015
How can I make the datetick count from 00:00 to 23:59. This below is dispalying 00:00 for every hour.
d= datetime(start_date):1/24:datetime(end_date)+1;
hours=hour(d);
scatter(hours,rand(size(d)));
t=datenum(d);
ax=gca;
ax.XTick=1:24;
datetick(ax,'x','HH:MM','keeplimits','keepticks')

  0 Comments

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 26 Feb 2015
The XTick values need to be date number for datetick to work, so maybe:
ax.XTick = ceil(ax.XLim(1)):1/24:floor(ax.XLim(2));
datetick(ax,'x','HH:MM','keeplimits','keepticks');

  5 Comments

Show 2 older comments
ccs
ccs on 27 Feb 2015
Thanks again Guillaume. Yes, I want just 24 ticks, from 00:00 to 23:59 so that i can be able to use it to plot measured data for the whole day (and for each day) for a given range of days. the last line gives this error:
>>ax.XTick = linspace(ceil(ax.XLim(1)), floor(ax.XLim(1), 24)); Error using floor Too many input arguments.
Guillaume
Guillaume on 27 Feb 2015
Yes, there was a typo, missing a bracket which you added in the wrong place. I've corrected my answer
ccs
ccs on 27 Feb 2015
>> d= datetime(start_date):1/24:datetime(end_date)+1;
>> hours=hour(d);
>> t=datenum(d);
>> scatter(hours,rand(size(d)));
>> ax=gca;
>> ax.XMinorTick='on';
>> ax.XTick = linspace(ceil(ax.XLim(1)), floor(ax.XLim(1)), 24);
While setting the 'XTick' property of Axes: Value must be a vector of type single or double whose values increase
Now it says the vector values aren't increasing. Sorry I can figure this out.

Sign in to comment.

Sign in to answer this question.