# Setting time ticks in plot

11 views (last 30 days)
Sepp on 22 Aug 2017
Answered: Peter Perkins on 23 Aug 2017
Hello
I have made a Matlab plot based on a data vector. On the x-axis should be the time and on the y-axis the value. For each value I have a timestamp in milliseconds. I would like to have on the x-axis a tick every say 5min, say 16:05, 16:10, 16:15 and so on (16:05 means 4pm and 5mins). The time distance between the values is not equal.
How can this be done?
An example of a data vector is (first column timestamp, second column value):
27634517312999, 1.1111809015274048
27658503234656, 0.7998865246772766
27662988099446 0.7806665897369385
27664499628612 0.7781039476394653
27950691973920 0.8562135696411133
27970195902662 0.8036822080612183
27973194252661 0.7908696532249451
....
Jan on 22 Aug 2017
Edited: Jan on 22 Aug 2017
If the first column are really milliseconds, the shown data have been recorded over almost 11 years. Plotting them with tick labels all 5 minutes will be very ugly.
(27973194252661 - 27634517312999) / 1000 ... % seconds
/ 86400 ... % days
/ 365 % years
Do you mean nano-seconds?

Jan on 22 Aug 2017
data = [27634517312999, 1.1111809015274048; ...
27658503234656, 0.7998865246772766; ...
27662988099446, 0.7806665897369385; ...
27664499628612, 0.7781039476394653; ...
27950691973920, 0.8562135696411133; ...
27970195902662, 0.8036822080612183; ...
27973194252661, 0.7908696532249451];
Time = data(:, 1) / (1000 * 86400); % Milliseconds -> datenum format
Ticks = (ceil(data(1,1) / 300e3) : floor(data(end,1) / 300e3)) * 300 / (1000 * 86400);
% Far too many ticks to be drawn!
plot(Time, data(:, 2));
datetick('x', 'HH:MM', 'keepticks');
##### 3 CommentsShow 1 older commentHide 1 older comment
Jan on 22 Aug 2017
When I do Time = data(:, 1) / (1000 * 86400); I'm getting 17397
for every timestamp value
Do you? Please post the corresponding code.
Time = data(:, 1) / (1000 * 86400);
Time(1)
319843.950381933
diff(Time)
277.614833993022
51.9081572916475
17.4945505324285
3312.41140402778
225.739915995393
34.7031249884167
I do not see a 17397 (in which units?)
If 27658503234656 means 18-Jun-2048 14:33:54 the number are in fact the milliseconds since 01-Jan-1970. Then, as said already, your data set concerns a period of 10.7 years and ticks every 5 minutes will be an unreadable pile of pixels.
Jan on 22 Aug 2017
@Sepp: Did you try my code then?

Peter Perkins on 23 Aug 2017
The numbers in your example data don't match the numbers in your explanation:
>> x = [ ...
27634517312999, 1.1111809015274048
27658503234656, 0.7998865246772766
27662988099446 0.7806665897369385
27664499628612 0.7781039476394653
27950691973920 0.8562135696411133
27970195902662 0.8036822080612183
27973194252661 0.7908696532249451];
>> data = array2table(x,'VariableNames',{'TimeStamp','X'});
>> data.TimeStamp = datetime(data.TimeStamp/1000,'ConvertFrom','posixtime')
data =
7×2 table
TimeStamp X
____________________ _______
13-Sep-2845 22:48:32 1.1112
18-Jun-2846 13:33:54 0.79989
09-Aug-2846 11:21:39 0.78067
26-Aug-2846 23:13:48 0.7781
21-Sep-2855 09:06:13 0.85621
04-May-2856 02:51:42 0.80368
07-Jun-2856 19:44:12 0.79087
As others have said, this spans 11 years, and labelling ticks at 5min intervals makes no sense. If you do have data that count ms since 1970, and that span about an hour, do the above, and then start from
plot(data.TimeStamp,data.X)
Depending on what release of MATLAB, setting the ticks to every 5min is different but generally easy.