Retrieve data from textfile
Show older comments
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 Comment
Walter Roberson
on 30 Aug 2017
Is that json format?
Answers (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 Comments
Yik Ka Yau
on 30 Aug 2017
Edited: Yik Ka Yau
on 30 Aug 2017
Walter Roberson
on 31 Aug 2017
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
on 5 Sep 2017
Walter Roberson
on 5 Sep 2017
You need to use
fid = fopen('NameOfTextFileGoesHere', 'rt');
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!