How to use textscan to read this .dat file?

Hi,
I fail to use textscan to read the following formatted data. What should be the correct argument to read it and let the same data type be in a single array? Thanks!
{"time":1431653957, "OD":0.0010, "Z":0.0000, "U":0}
{"time":1431654017, "OD":0.0007, "Z":0.0000, "U":0}
{"time":1431654077, "OD":-0.0005, "Z":0.0000, "U":0}
{"time":1431654137, "OD":-0.0015, "Z":0.0000, "U":0}

3 commentaires

Stephen23
Stephen23 le 17 Mai 2015
Modifié(e) : Stephen23 le 17 Mai 2015
Instead of giving us this short and probably edited version of the text, please simply upload the file using the paperclip button. You will need to push both Choose file and Attach file buttons.
It is much easier for us to work with the real data file, rather than trying to interpret this text.
Hsinho
Hsinho le 17 Mai 2015
Hi
I want to read another more complicated formatted data.
It looks like
{"timestamp": 1431654053, "ods": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], "u": [7, 7, 7, 7, 7, 7, 7, 7], "z": ["0.0000", "0.0000", "0.0000", "0.0000", "0.0000", "0.0000", "0.0000", "0.0000"]}
I fail again to have a correct argument of textscan. The original data file is uploaded.
Stephen23
Stephen23 le 17 Mai 2015
Modifié(e) : Stephen23 le 17 Mai 2015
Indeed this is a much more complicated file format. Some more information would help:
  • is the number of elements in the vectors constant?
  • should numeric values with quotation marks be imported as strings?
  • what is the desired output format?

Connectez-vous pour commenter.

 Réponse acceptée

Stephen23
Stephen23 le 17 Mai 2015
Modifié(e) : Stephen23 le 17 Mai 2015
It is not clear exactly how this file is formatted, as the text on the page is not a good way to represent files. Please upload the actual data file.
Based on the text in the question I created a small data file (attached), and then a small script containing this code:
fmt = ['{',repmat('%*s%f',1,4),'}'];
fid = fopen('temp.txt','rt');
A = cell2mat(textscan(fid,fmt,'Delimiter',':,'));
fclose(fid);
Which reads all of the numeric data correctly:
>> A
A =
1.4317e+009 0.001 0 0
1.4317e+009 0.0007 0 0
1.4317e+009 -0.0005 0 0
1.4317e+009 -0.0015 0 0

1 commentaire

Hsinho
Hsinho le 17 Mai 2015
Thank you very much! The data is a .dat file and its content looks exactly what has been paste but with more lines in the same format.
Your answer has solved my question. :-)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Export dans Centre d'aide et File Exchange

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by