Read text file efficiently
Mostrar comentarios más antiguos
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 comentarios
Stephen23
el 24 de En. de 2019
Image Analyst
el 25 de En. de 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.
Respuestas (2)
Walter Roberson
el 25 de En. de 2019
fid = fopen('YourFile.txt', 'rt');
data = cell2mat( textscan(fid, '%f/%f/%f %f:%f %f %f %f', 'collectoutput', true);
fclose(fid);
3 comentarios
Walter Roberson
el 26 de En. de 2019
filename = 'test.txt';
fid = fopen(filename, 'rt');
location_cell = textscan(fid, 'Location: %f° %f'' %f" %c %f° %f'' %f" %c%*[^\n]', 1, 'Headerlines', 1);
data = cell2mat( textscan(fid, '%f/%f/%f %f:%f %f %f %f', 'collectoutput', true, 'Headerlines', 6) );
fclose(fid);
lat = location_cell{1} + location_cell{2}/60 + location_cell{3}/3600;
if strcmpi(location_cell{4}, 's'); lat = -lat; end
long = location_cell{5} + location_cell{6}/60 + location_cell{7}/3600;
if strcmpi(location_cell{8}, 'w'); long = -long; end
%outputs: lat, long, data
Mjan88
el 27 de En. de 2019
Walter Roberson
el 27 de En. de 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.
Mjan88
el 26 de En. de 2019
0 votos
Categorías
Más información sobre Text Data Preparation 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!