Retrieve data from textfile
Mostrar comentarios más antiguos
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 comentario
Walter Roberson
el 30 de Ag. de 2017
Is that json format?
Respuestas (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 comentarios
Yik Ka Yau
el 30 de Ag. de 2017
Editada: Yik Ka Yau
el 30 de Ag. de 2017
Walter Roberson
el 31 de Ag. de 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
el 5 de Sept. de 2017
Walter Roberson
el 5 de Sept. de 2017
You need to use
fid = fopen('NameOfTextFileGoesHere', 'rt');
Categorías
Más información sobre String Parsing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!