Clear Filters
Clear Filters

For tabularTextDatastore, how do I specify a column as a datetime or duration

8 views (last 30 days)
I have many csv files of time history data. If I create a tabularTextDatastore:
ttds = tabularTextDatastore("C:/myfiles");
myttds = ttds.preview
I get:
myttds = 8x414 table
Time TimeOfDay WT1DCBusVoltage1 WT10ACCurrentAPhase
__________ ______________ _________________ ___________________
63.9609375 59117.1640625 0.155702099204063 1.50458014011383
63.9921875 59117.203125 0.204921007156372 1.51381540298462
64.0234375 59117.234375 0.193629086017609 1.50028717517853
64.0546875 59117.26171875 0.137909501791 1.49814927577972
64.0859375 59117.29296875 0.137250736355782 1.50692522525787
64.1171875 59117.32421875 0.16432049870491 1.51645302772522
64.1484375 59117.35546875 0.159727975726128 1.50788652896881
64.1796875 59117.38671875 0.159778669476509 1.51117658615112
(the rest of the columns are omitted)
but I want it to be a timetable datastore, so I tried:
ttds = tabularTextDatastore(fs,"RowTimes",'Time');
and get error:
The row times variable 'Time' must be of type datetime or duration
How do I specify that "Time" is a duration of seconds? I tried to convert the format using:
istime = cellfun(@(x)isequal(x,"Time"),ttds.SelectedVariableNames);
ttds.SelectedFormats(istime) = {'%{ss.SSSSSSSSSS}T'};
myttds = ttds.preview
but still get an error:
"ss.SSSSSSSSSS" is not a valid duration format.

Answers (1)

Gayatri Rathod
Gayatri Rathod on 1 Mar 2023
Hi Matthew,
To specify a column as a datetime or duration in a tabularTextDatastore, you need to first convert the column to the appropriate data type before creating the datastore.
Below are the steps to convert the "Time" column to a duration of seconds:
% Load the data into a table
mytable = readtable("C:/myfiles/mydata.csv");
% Convert the "Time" column to a duration of seconds
mytable.Time = seconds(mytable.Time);
% Create a timetable from the table
mytimetable = table2timetable(mytable, 'RowTimes', 'Time');
% Save the timetable as a MAT file
save("mytimetable.mat", "mytimetable");
% Load the timetable from the MAT file
mytimetable = load("mytimetable.mat", "mytimetable").mytimetable;
% Create a tabularTextDatastore from the timetable
ttds = tabularTextDatastore("mytimetable.mat", "ReadFcn", @load, "SelectedVariableNames", {'Time', 'WT1DCBusVoltage1', 'WT10ACCurrentAPhase'});
This will create a tabularTextDatastore that only includes the "Time", "WT1DCBusVoltage1", and "WT10ACCurrentAPhase" columns.
You can read more about the tabularTextDatastore, seconds and table2timetable functions from the following documentations:  tabulartextdatastore Function, seconds Function, table2timetable Function.
Hope it helps!
Regards,
Gayatri Rathod

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!