Retrieve data from textfile
古いコメントを表示
I am trying to retrieve data from this text file. Ultimately, I want to get the information for date (e.g.6/9/2016) and absolute time (e.g. 20.000761) using Matlab. How do I do that?
Here is an example:
{"metadata":{"date":"6/9/2016 4:25:43 PM","TotalShrooms":{"AbsoluteTime":20.000761,"Value":0}},"hits":{},"recording":false}
Thank you.
1 件のコメント
Walter Roberson
2017 年 8 月 30 日
Is that json format?
回答 (1 件)
fmt=['{"metadata":{"date":"' '{%{M/d/yyyy hh:mm:ss a}D' '","TotalShrooms":{"AbsoluteTime": %f %*[^\n]'];
% file open preamble code goes here to get valid file handle fid
data=textscan(fid,fmt);
will return cell array containing the datetime and value data variables.
UPDATE: Fixup of parens mismatch...
4 件のコメント
Yik Ka Yau
2017 年 8 月 30 日
編集済み: Yik Ka Yau
2017 年 8 月 30 日
Walter Roberson
2017 年 8 月 31 日
textscan cannot handle spaces in a date format unless 'whitespace' has been set to exclude space. That might actually work out in the given example -- however if there are any other spaces on the line then it would fail. For example if it were "AbsoluteTime": 20.000761 instead of "AbsoluteTime":20.000761 then setting the 'whitespace' to exclude space would lead to failure in the later space.
Yik Ka Yau
2017 年 9 月 5 日
Walter Roberson
2017 年 9 月 5 日
You need to use
fid = fopen('NameOfTextFileGoesHere', 'rt');
カテゴリ
ヘルプ センター および File Exchange で String Parsing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!