Removing characters from a datetime format to make it recognizable to MATLAB

13 views (last 30 days)
I have a large data file that includes date information that I would like to convert to a timetable. This is nothing new but the format used by SMT Research is not recognized in Matlab. They use '2022-08-31 16:59:51+00'. The last deliniation remains unused for all data enties, alway '+00' so I would like to remove it. I have atempted to use the erase and replace function but i get the error "First argument must be text." Can someone help with this. Code is below and the imput file and import function are attached.
%% Call imprort function
data = importfileANL("export-4-5-23.csv"); %inputs the specified CSV file as a table
% import function only works with the standard output of Analytics
%% Convert date info from a date string to a datetime array
data = replace(data.DateTime, "+00",""); % removes proprietary millisecond demotation not recognized in matlab
data.DateTime = datetime(data.DateTime,'InputFormat','yyyy-MM-dd HH:mm:ss');

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 8 Apr 2023
The column with date time values is stored as a categorical array. Convert it to a string array and then use replace (you can also use erase)
data = importfileANL("export-4-5-23_shortened.csv");
data.DateTime = replace(string(data.DateTime),"+00","")
data = 16×7 table
DateTime SensorID Name NodeID Input Value EngineeringUnit _____________________ __________ ____________________ ______ _____ __________ _______________ "2022-08-31 16:59:51" 3.7754e+05 New SMT-A2 Sensor 31462 20 1e+09 -2.4977e+05 "2022-08-31 16:59:51" 3.7755e+05 New SMT-A2 Sensor 31462 22 1e+09 -2.4976e+05 "2022-08-31 16:59:51" 3.7754e+05 New SMT-A2 Sensor 31462 21 1e+09 -2.4975e+05 "2022-08-31 16:59:51" 3.7754e+05 Internal Temperature 31462 5 1.13e+05 22.546 "2022-08-31 16:59:51" 3.7754e+05 Internal RH 31462 6 2.4045e+06 59.825 "2022-08-31 16:59:51" 3.7754e+05 Battery 31462 7 4355 4355 "2022-08-31 16:59:51" 3.7754e+05 New SMT-A2 Sensor 31462 17 1e+09 -2.4976e+05 "2022-08-31 16:59:51" 3.7754e+05 New SMT-A2 Sensor 31462 18 1e+09 -2.4976e+05 "2022-08-31 16:59:51" 3.7754e+05 New SMT-A2 Sensor 31462 19 1e+09 -2.4977e+05 "2022-08-31 17:00:37" 3.7754e+05 New SMT-A2 Sensor 31462 17 1e+09 -2.4976e+05 "2022-08-31 17:00:37" 3.7754e+05 New SMT-A2 Sensor 31462 18 1e+09 -2.4976e+05 "2022-08-31 17:00:37" 3.7754e+05 Battery 31462 7 4355 4355 "2022-08-31 17:00:37" 3.7755e+05 New SMT-A2 Sensor 31462 22 1e+09 -2.4976e+05 "2022-08-31 17:00:37" 3.7754e+05 New SMT-A2 Sensor 31462 21 1e+09 -2.4975e+05 "2022-08-31 17:00:37" 3.7754e+05 New SMT-A2 Sensor 31462 20 1e+09 -2.4977e+05 "2022-08-31 17:00:37" 3.7754e+05 New SMT-A2 Sensor 31462 19 1e+09 -2.4977e+05
data.DateTime = datetime(data.DateTime,'InputFormat','yyyy-MM-dd HH:mm:ss')
data = 16×7 table
DateTime SensorID Name NodeID Input Value EngineeringUnit ____________________ __________ ____________________ ______ _____ __________ _______________ 31-Aug-2022 16:59:51 3.7754e+05 New SMT-A2 Sensor 31462 20 1e+09 -2.4977e+05 31-Aug-2022 16:59:51 3.7755e+05 New SMT-A2 Sensor 31462 22 1e+09 -2.4976e+05 31-Aug-2022 16:59:51 3.7754e+05 New SMT-A2 Sensor 31462 21 1e+09 -2.4975e+05 31-Aug-2022 16:59:51 3.7754e+05 Internal Temperature 31462 5 1.13e+05 22.546 31-Aug-2022 16:59:51 3.7754e+05 Internal RH 31462 6 2.4045e+06 59.825 31-Aug-2022 16:59:51 3.7754e+05 Battery 31462 7 4355 4355 31-Aug-2022 16:59:51 3.7754e+05 New SMT-A2 Sensor 31462 17 1e+09 -2.4976e+05 31-Aug-2022 16:59:51 3.7754e+05 New SMT-A2 Sensor 31462 18 1e+09 -2.4976e+05 31-Aug-2022 16:59:51 3.7754e+05 New SMT-A2 Sensor 31462 19 1e+09 -2.4977e+05 31-Aug-2022 17:00:37 3.7754e+05 New SMT-A2 Sensor 31462 17 1e+09 -2.4976e+05 31-Aug-2022 17:00:37 3.7754e+05 New SMT-A2 Sensor 31462 18 1e+09 -2.4976e+05 31-Aug-2022 17:00:37 3.7754e+05 Battery 31462 7 4355 4355 31-Aug-2022 17:00:37 3.7755e+05 New SMT-A2 Sensor 31462 22 1e+09 -2.4976e+05 31-Aug-2022 17:00:37 3.7754e+05 New SMT-A2 Sensor 31462 21 1e+09 -2.4975e+05 31-Aug-2022 17:00:37 3.7754e+05 New SMT-A2 Sensor 31462 20 1e+09 -2.4977e+05 31-Aug-2022 17:00:37 3.7754e+05 New SMT-A2 Sensor 31462 19 1e+09 -2.4977e+05

More Answers (1)

Steven Lord
Steven Lord on 8 Apr 2023
You can include literal characters in the InputFormat argument of a call to datetime. See the "Date and Time from Text with Literal Characters" example on the datetime documentation page.
s = '2022-08-31 16:59:51+00'
s = '2022-08-31 16:59:51+00'
d = datetime(s, InputFormat = "yyyy-MM-dd HH:mm:ss+00")
d = datetime
31-Aug-2022 16:59:51
If you want to read in the data with the numbers after the + as the fractional seconds you can do that too.
s = '2022-08-31 16:59:51+79'
s = '2022-08-31 16:59:51+79'
I'll display just the time part of d, but the date part is still present.
d = datetime(s, InputFormat = "yyyy-MM-dd HH:mm:ss+SS", Format = "HH:mm:ss.SS")
d = datetime
16:59:51.79
  4 Comments
Dyuman Joshi
Dyuman Joshi on 9 Apr 2023
@Peter Perkins note that the OP is using their own method to import data, and thus the data for the datetime values is imported as categorical.

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!