Read in dates (for timestamp) from file names

4 ビュー (過去 30 日間)
Heidi Hirsh
Heidi Hirsh 2019 年 3 月 26 日
編集済み: dpb 2019 年 3 月 26 日
I am trying to read in data and the measurement timestamps do not include the dates. The filenames include the date/time when the file was started. How can save the date from the filename to use it in the timestamps for each measurement?
I was previously doing this:
filepath = 'HR3_181920/PH/';
filelist = dir(strcat(filepath,'*.txt'));
file_timestamps =cell2mat({filelist.datenum});
But this doesn't work because it is taking the date from the structure for the datenum value which I think is actually the time when the file ends (not the beginning). I have attached a file of the resulting structure which includes the names of the files I am trying to work with.
These are the 'file_timestamps' that I currently get when I run those line. Notice they match the date column but not the information in the name of the file.
  2018 7 18 12 49 50
2018 7 18 12 54 48
2018 7 18 17 3 58
2018 7 19 0 0 2
2018 7 19 11 50 12
2018 7 19 11 54 18
2018 7 19 18 5 4
2018 7 19 18 25 20
2018 7 19 19 50 54
2018 7 19 20 26 12
2018 7 19 20 26 36
2018 7 19 20 27 20
2018 7 19 20 28 6
2018 7 19 20 28 54
2018 7 19 20 29 58
2018 7 19 20 32 28

採用された回答

dpb
dpb 2019 年 3 月 26 日
編集済み: dpb 2019 年 3 月 26 日
Just parse the filename itself...
fmt='yyyyMMMdd_HH-mm-ss';
tstr=cellstr(strvcat(d.name));
file_timestamps=cellfun(@(x) datetime(x(1:18),'InputFormat',fmt),tstr);
or just do each inside the loop and can do without the conversion to cellstr array and cellfun to just parse each substring.
ERRATUM: I had a t test array; thought better variable name wise but didn't fixup the second reference to match...
...
file_timestamp=datetime(d(i).name(1:18),'InputFormat',fmt);
...
  1 件のコメント
Heidi Hirsh
Heidi Hirsh 2019 年 3 月 26 日
what is t?

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 3 月 26 日
file_timestamps = datetime( regexp({filelist.name}, '^[^_]*', 'match', 'once'), 'InputFormat', 'yyyyMMMdd');

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by