Time and date fields not correclty imported using readmatrix

37 ビュー (過去 30 日間)
Rakshith Badarinath
Rakshith Badarinath 2021 年 4 月 4 日
I am trying to import csv and dat files that have columns with date and time. Here is a snapshot of csv file:
And dat file:
I am using "readmatrix" method for import. However, upon import, the date and time fields are converted to NaN. Is there a way to read these files while preserving time format? I intend to do some timestamp based filtering and plotting, so the resloution of timestamp is important.

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 4 月 4 日
編集済み: Cris LaPierre 2021 年 4 月 4 日
You cannot mix data types when you use readmatrix, meaning all the data you import must be of the same data type. That is why you are getting NaN and not NaT.
Use readtable or readtimetable instead. In a table, each column can be a different data type.
  4 件のコメント
Cris LaPierre
Cris LaPierre 2021 年 4 月 4 日
You would have to use a second read command to capture just row 3. Perhaps textscan? I'm not feeling motivated enough to transcribe your image for testing, so here's a best guess at what that might be.
fid = fopen('filename.dat');
% Skip the first string, then read in time as a duration
tm = textscan(fid,'%*s %{hh:mm:ss.SSS}T',1,'HeaderLines',2)
fclose(fid);
tm will be a cell array. You can extract the time value from the cell array like this.
tm=tm{:}
Then use your readtable command to get the matrix of data.
With all your dates as datetimes or durations, you can perform Date and Time arithmetic.
Rakshith Badarinath
Rakshith Badarinath 2021 年 4 月 4 日
Thanks. I ended up using fopen with fegetl and regexp to extract the time cell array and converted it to duration. textscan seems compact, will give it a try.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStandard File Formats についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by