# convert epoch time to yyyyMMDDHHMMSS

6 views (last 30 days)
Sausan Ahmad on 17 Oct 2020
Answered: Cris LaPierre on 18 Oct 2020
I want to plot sensor data from csv file (between rows 150 and 220) using this code:
clear
close all
file_name = 'HPP2.csv'
% read the timing of each data set:
% Transform the calendar time into epoch time:
for uu = 2:(size(A,1)+1)
line = text{uu};
time_axis(uu-1) = cdflib.computeEpoch([str2num(line(1:4)) str2num(line(6:7)) str2num(line(9:10)) str2num(line(12:13)) str2num(line(15:16)) str2num(line(18:19)) 0]);
end
start=150
last=220
x=time_axis(start:last);
y1 = A(start:last,80);
y2 = A(start:last,58);
Y3=A(start:last,55)
% Top two plots
tiledlayout(2,2)
nexttile
plot(x,y1)
xlabel('epoch_time')
ylabel('mass')
nexttile
plot(x,y2)
xlabel('epoch_time')
ylabel('temperature')
My problem is that I want to display the actual time/date on the x axis instead of the epoch time? I know the actual time/date correspond to rows 150 and 220?
how can I make convert epoch time back to real time/date format please?

Cris LaPierre on 17 Oct 2020
Use datetime with the 'epochtime' specified.
% Example: Return the number of days since January 1, 2000.
T = datetime(X,'ConvertFrom','epochtime','Epoch','2000-01-01')
% Example: Return the number of days since January 1, 2000, where X represents the time in milliseconds since or before that date.
T = datetime(X,'ConvertFrom','epochtime','Epoch','2000-01-01','TicksPerSecond',1000)

Sausan Ahmad on 17 Oct 2020
Many thanks for your reply. The code you have sent unfortunately didn't work. I am new to MATLAB, I want to plot mass over time but I want the time to show in real time format say I want the date between (2020-26-04 and 2020-26-08, these dates corespond to rows 150 and 220 ) is ther an easy way to do it please?
I have used cdflib.epochBreakdown(x(1)) to show the time correspond to each row I want.
Cris LaPierre on 17 Oct 2020
If you load the data as datetime, then plotting is as easy as plot(x,y). You do not need to do anything special. If you want to modify the format so something specific, you can use xtickformat.

Sausan Ahmad on 17 Oct 2020
Please find attached my section of my data

Cris LaPierre on 18 Oct 2020
Just as an example, here is one way to read it in.
opts = detectImportOptions("HPP2.xlsx");
% Keep variable names as they are
opts.VariableNamingRule = "preserve";
% Set options to corretly read time: 2020-06-24T11:19:01Z
opts = setvartype(opts,"time","datetime");
opts = setvaropts(opts,"time","InputFormat",'yyyy-MM-dd''T''HH:mm:ss''Z');
% Manually select columns to import
opts.SelectedVariableNames = [1,6,18,32,35,38:41,43:45,47,56,58,59,78,81];
% Load data as a timetable
% Set display format of time
data.time.Format = 'ddMMMyy HH:mm:ss'
data = 47x17 timetable
time Voltage - bv (V) rssi s bin 122 173 s bin 173 224 s bin 224 276 s bin 276 327 s bin 327 378 s bin 378 429 s bin 429 480 s bin 480 532 s bin 532 583 s bin 71 122 snr Temperature - t (Â°C) Temperature - t (Â°C)_1 w v Weight - weight (kg) ________________ ________________ ____ _____________ _____________ _____________ _____________ _____________ _____________ _____________ _____________ _____________ ____________ ____ _____________________ _______________________ __________ ____________________ 24Jun20 11:19:01 NaN -75 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 8.2 NaN NaN NaN NaN 24Jun20 11:33:07 3.443 -85 12 10 6 7 8 22 5 4 1 19 9 30.93 31 9.6538e+05 25.67 24Jun20 11:48:07 3.45 -86 12 8 9 9 11 27 9 8 9 19 12.8 30.56 30.56 9.6538e+05 25.67 24Jun20 12:03:07 3.451 -83 11 8 7 7 6 9 31 20 2 17 9.2 30.31 29.25 9.6532e+05 25.668 24Jun20 12:18:07 3.468 -85 11 7 7 7 6 7 32 17 3 16 13.8 30.37 30.62 9.6499e+05 25.657 24Jun20 12:33:07 3.461 -85 12 10 6 7 5 5 24 27 1 14 13.5 30.5 30.62 9.6416e+05 25.629 24Jun20 13:18:07 3.462 -85 9 9 7 7 17 13 6 8 22 18 12 31 31.75 9.6429e+05 25.633 24Jun20 13:33:07 3.466 -87 10 9 7 8 22 7 5 5 12 17 13.5 30.75 32.31 9.6382e+05 25.617 24Jun20 13:48:07 3.469 -83 9 11 7 9 22 9 6 7 5 16 11.8 30.56 32.56 9.6403e+05 25.625 24Jun20 14:03:08 3.465 -85 10 8 7 10 20 7 5 4 2 14 13.2 30.62 31 9.6315e+05 25.594 24Jun20 14:18:13 3.473 -89 11 7 7 16 16 7 3 5 2 17 13.2 30.5 29.93 9.633e+05 25.6 24Jun20 14:33:07 3.469 -91 7 7 9 20 13 5 5 4 2 14 13 30.62 31.25 9.6381e+05 25.617 24Jun20 14:48:11 3.468 -85 8 7 6 22 9 6 4 4 1 14 13.5 30.68 30.56 9.6335e+05 25.601 24Jun20 15:03:07 3.472 -87 12 8 9 22 8 4 3 3 1 17 12.8 30.81 34.37 9.6308e+05 25.592 24Jun20 15:18:07 3.475 -95 9 8 9 21 9 5 5 3 1 13 11.8 30.75 33.37 9.6295e+05 25.588 24Jun20 15:33:07 3.472 -88 8 7 11 21 7 6 3 5 1 16 12.2 30.75 35.37 9.6298e+05 25.589