![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/174359/image.jpeg)
Setting time on X-axis for over 24 hours
4 views (last 30 days)
Show older comments
I have to plot time on X-axis with the time close to 10^6 secs. how can I show them in hours? I tried datetick('x','HH:MM') But it sets the time to 00:00
What i would like to see is something like this 'HHH:MM'
Thanks in advance
0 Comments
Answers (1)
dpb
on 25 May 2016
Edited: dpb
on 26 May 2016
Time axes in Matlab are in either (traditional) datenum to use datetick or the newer time class (R2014?, maybe?). But, neither knows how to handle hours other than traditional 24-hr/day so that doesn't help...
Put the following into a helper function so you can just call it with the time vector but given an array of times in seconds, t, the following should do what you're looking for--
r=t/3600; % time in hours and fractions to save multiple divides
hr=fix(r); % the whole hour portion for each
mn=round((r-hr)*60); % and the minute in integer
Example use --
>> t % similar to your start for short example
t =
2974800 2974860 2974920 2974980 2975040 2975100
>> plot(r,randn(size(r))) % a typical plot
>> xlim([r(1) r(end)]) % range the axes over same values
>> set(gca,'xtick',r) % and set tick marks to match
>> set(gca,'xticklabel', ...
cellstr(arrayfun(@(h,m) sprintf('%03d:%02d',h, m),hr,mn,'uni',0)))
The last is the "trick" to format the hr:min for labeling axis tick per desire of HHH:MM
You'll need to select the tick marks at some interval much larger than 1 as illustrated here to not have too many; whatever is differential between N minutes would be the logical way to do this.
Sorry I didn't catch on to the real problem first pass...
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/174359/image.jpeg)
2 Comments
dpb
on 26 May 2016
Oh, old eyes missed the third 'H' on the initial posting, sorry. I don't know if the new '%D' format string will convert to number of hours cumulative or not; the traditional datestr (and hence datetick) cannot as I now presume you'd already discovered.
Best I can think of is to use the internal datenum logic to convert with rollover to generate the actual date components and then build the actual time as wanted...
Sample starting with your starting time and adding some to it; you'd use your actual seconds vector of course...
[y,m,d,h,mn,s]=datevec(datenum(2016,0,0,0,0,2974800);
Then build the hourly fractional number from these--
>> (sum(eomday(y,1:m-1))+d)*24+h+mn/60
ans =
826.3333
>>
Alternatively, just begin w/ your seconds values and divide and remainder and repeat for the smaller time periods.
See Also
Categories
Find more on Dates and Time 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!