how to read mixed date and data

2 次查看(过去 30 天)
How to read this data file:
2020/11/15 07:30:55 27.50 124
2020/11/15 07:40:55 27.63 114
2020/11/15 07:50:55 27.71 95

采纳的回答

Jeremy Hughes
Jeremy Hughes 2021-9-27
I made some corrections in the comments above, but this is how I would do this more robustly using partial detection with detectImportOptions.
filename = "example.txt";
opts = detectImportOptions(filename,"Delimiter","\t");
opts = setvaropts(opts,1,"Type","datetime","InputFormat","uuuu/MM/dd HH:mm:ss");
T = readtable(filename,opts)
T = 3×3 table
Var1 Var2 Var3 ___________________ _____ ____ 2020/11/15 07:30:55 27.5 124 2020/11/15 07:40:55 27.63 114 2020/11/15 07:50:55 27.71 95
  2 个评论
DwiNY
DwiNY 2021-9-27
Dear Jeremy Hughes,
Thank you very much
DwiNY
DwiNY
DwiNY 2021-9-27
Jeremy,
If you could make more a general, i.e. input file with XX number of lines of header would be appreciate.
Thank you

请先登录,再进行评论。

更多回答(1 个)

the cyclist
the cyclist 2021-9-24
I would try the readtable function.
You can specify the input formating if needed.
  4 个评论
DwiNY
DwiNY 2021-9-27
Finally, I able to read the file correctly:
fid=fopen('filename.txt','rt');
formatSpec = '%{yyyy/mm/dd}D %{HH:mm:ss}D %f%f ';
delimiter = {'\t',' '};
dataArray = textscan(fid, formatSpec, 'Delimiter', delimiter,'MultipleDelimsAsOne', true, 'ReturnOnError', false);
fclose(fid)
Jeremy Hughes
Jeremy Hughes 2021-9-27
If you're using format, there are issues with your format in both cases: mm cannot be both months and minutes, you want MM.
Also, you probably want the second part as duration if you're parsing these as separate fields.
try this: formatSpec = '%{yyyy/MM/dd}D{hh:mm:ss}T%f%f'
But I like your first approach better:
delimiter = '\t'; %no space
formatSpec = '%{yyyy/MM/dd HH:mm:ss}D%f%f'

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by