Converting table data to datetime

81 visualizzazioni (ultimi 30 giorni)
I have data in a .csv file. The second column contains time data. I want to convert it to datetime.
My code is:
tbldata=readtable('pipe2_st.csv');
tbldataarray=table2cell(tbldata);
data1 = tbldataarray (:,2);
t = datetime(data1,'InputFormat', '[HH:mm:ss.SSS]');
The data in 'data1' is:
{[01:05:17.319]}
{[01:05:17.328]}
{[01:05:17.338]}
{[01:05:17.347]}
{[01:05:17.357]}
{[01:05:17.365]}
{[01:05:17.385]}
{[01:05:17.394]}
{[01:05:17.403]}
{[01:05:17.412]}
{[01:05:17.422]}
{[01:05:17.432]}
{[01:05:17.441]}
{[01:05:17.450]}
{[01:05:17.460]}
{[01:05:17.469]}
{[01:05:17.478]}
{[01:05:17.488]}
{[01:05:17.497]}
{[01:05:17.506]}
{[01:05:17.516]}
{[01:05:17.525]}
{[01:05:17.534]}
{[01:05:17.544]}
It gives the error:
Error using datetime (line 672)
Input data must be a numeric array, a string array, a cell array containing character vectors, or a char matrix.
Kindly help in resolving the error.
  1 Commento
Stephen23
Stephen23 il 12 Apr 2023
@Kanica Sachdev: please upload the original CSV file by clicking the paperclip button.

Accedi per commentare.

Risposta accettata

Cris LaPierre
Cris LaPierre il 12 Apr 2023
Modificato: Cris LaPierre il 12 Apr 2023
You need a date to make it a datetime (it will automatically use today if you don't supply one). Perhaps you want to use duration instead?
readtable should automatically read in the data as a duration, which has all the same benefits when it comes to tima as a datetime. There is no need to use table2cell. Instead, see how to access data in tables.
Coverting what you show as output to a csv file (attached), here is what my code would be.
tbldata=readtable('pipe2_st.csv')
tbldata = 24×1 table
Var1 ____________ 01:05:17.319 01:05:17.328 01:05:17.338 01:05:17.347 01:05:17.357 01:05:17.365 01:05:17.385 01:05:17.394 01:05:17.403 01:05:17.412 01:05:17.422 01:05:17.432 01:05:17.441 01:05:17.450 01:05:17.460 01:05:17.469
% Extract the Var1 variable from the table
tbldata.Var1
ans = 24×1 duration array
01:05:17.319 01:05:17.328 01:05:17.338 01:05:17.347 01:05:17.357 01:05:17.365 01:05:17.385 01:05:17.394 01:05:17.403 01:05:17.412 01:05:17.422 01:05:17.432 01:05:17.441 01:05:17.450 01:05:17.460 01:05:17.469 01:05:17.478 01:05:17.488 01:05:17.497 01:05:17.506 01:05:17.516 01:05:17.525 01:05:17.534 01:05:17.544

Più risposte (1)

dpb
dpb il 12 Apr 2023
Modificato: dpb il 12 Apr 2023
tbldata=readtable('pipe2_st.csv');
tbldataarray=table2cell(tbldata);
Don't convert the table to a cell array, use the table you've already got.....the form of the data output (a time string inside the square brackets all inside the curly braces) indicates readtable read the column as a duration variable already; you don't need to do anything with it other than perhaps combine it with the date ithat one presumes is in the first column.
I put the first few lines of your data above into a file with the above assumption...
>> tT=readtable('test.csv');
>> head(tT)
ans =
8×1 table
Var1
____________
01:05:17.319
01:05:17.328
01:05:17.338
01:05:17.347
01:05:17.357
01:05:17.365
01:05:17.385
01:05:17.394
>>
Then did the nasty thing of turning a perfectly good table into a cell array...
>> table2cell(ans)
ans =
8×1 cell array
{[01:05:17.319]}
{[01:05:17.328]}
{[01:05:17.338]}
{[01:05:17.347]}
{[01:05:17.357]}
{[01:05:17.365]}
{[01:05:17.385]}
{[01:05:17.394]}
>>
That's where the artifacts of the {[...]} came from, you just didn't look at the content of the table variables themselves...
>> isduration(tT.Var1)
ans =
logical
1
>> 1

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by