Read text file efficiently

12 visualizzazioni (ultimi 30 giorni)
Mjan88
Mjan88 il 24 Gen 2019
Commentato: Walter Roberson il 27 Gen 2019
Hi all,
Does anybody has an idea how to efficiently read a *.txt-file in following format:
1/ 2/2019 01:30 -1.59 0.39 253
1/ 2/2019 01:45 -1.78 0.35 250
1/ 2/2019 02:00 -1.94 0.31 246
1/ 2/2019 02:15 -2.07 0.26 240
I would like to have 8 columns (day, month, year,hour, minute, param1, param2, param3).
The file also has some headers btw.
Like to hear your advise!
  2 Commenti
Image Analyst
Image Analyst il 25 Gen 2019
Don't forget to attach your text file again, if you still need help.
There are lots of input options, like importdata(), textscan(), etc. if readtable() doesn't work.

Accedi per commentare.

Risposte (2)

Walter Roberson
Walter Roberson il 25 Gen 2019
fid = fopen('YourFile.txt', 'rt');
data = cell2mat( textscan(fid, '%f/%f/%f %f:%f %f %f %f', 'collectoutput', true);
fclose(fid);
  3 Commenti
Mjan88
Mjan88 il 27 Gen 2019
Thanks, Walter Roberson. This works fine, except that for some kind of reason the first two data-lines are skipped. I don't know what causes this. Do you have an idea?
Walter Roberson
Walter Roberson il 27 Gen 2019
You could try reducing HeaderLines to 4 on the second textscan. But I tested on your supplied file and the output looked fine to me.

Accedi per commentare.


Mjan88
Mjan88 il 26 Gen 2019
Thanks for the suggestions. Textscan seems to work better indeed, IF I do not have headers. Like Image Analyst already suggested, hereby the textfile itself. Does anybody have an idea how to obtain longitude, latitude, and the data itself (day, month, year,hour, minute, param1, param2, param3)? Thanks!

Categorie

Scopri di più su Text Data Preparation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by