How to ignore the headerline

60 vues (au cours des 30 derniers jours)
Thar
Thar le 17 Déc 2018
Modifié(e) : Guillaume le 18 Déc 2018
Hi all.
I have 173 txt files look like:
* number etc......
20 44.581273 131.198920 45.413450 6/9/2018 8.301089 700 800 20
20 37.821101 149.679660 52.175950 6/9/2018 9.231156 600 600 20
20 34.834385 166.779660 55.163450 6/9/2018 9.937562 700 700 20
20 35.097478 195.417160 54.900950 6/9/2018 11.030783 800 800 20
I want to take these data and create a matrix with 11 columns ignoring the headerline.
I did it:
format1='%f %f %f %f %f/%f/%f %f %f %f %f';
files=dir('*.txt');
N=length(files);
for k = 1:N
fid = fopen(files(k).name,'r');
A=textscan(fid,format1,'Commentstyle','*');
B{k,1}=cell2mat(A);
%clear A
fclose(fid);
end
but I take empty matrix B.
Any ideas??

Réponses (1)

Guillaume
Guillaume le 17 Déc 2018
Your file format looks like something that readtable should have no problem figuring out, so try:
files = dir('*.txt');
filesdata = cell(size(files)); %preallocate the cell array that receive the file content (your badly named B)
for fileidx = 1:numel(files)
filesdata{fileidx} = readtable(files(fileidx).name);
end
If readtable needs some help, then either:
filesdata{fileidx} = readtable(files(fileidx).name, 'ReadVariableNames', false);
or
filesdata{fileidx} = readtable(files(fileidx).name, 'ReadVariableNames', false, 'HeaderLines', 1);
should get it to work.
  3 commentaires
Guillaume
Guillaume le 18 Déc 2018
Please attach an example file. As said, readtable should have no problem identifying the format of your file so if it doesn't that's because there's something peculiar about it. The only way to know for sure is with an actual file.
Guillaume
Guillaume le 18 Déc 2018
Modifié(e) : Guillaume le 18 Déc 2018
readtable (R2018b) has no problem identifying the columns of your text file:
>> t = readtable('001.txt')
Warning: The DATETIME data was created using format 'MM/dd/uuuu' but also matched 'dd/MM/uuuu'.
To avoid ambiguity, use a format character vector. e.g. '%{MM/dd/uuuu}D'
> In table/readTextFile>textscanReadData (line 557)
In table/readTextFile (line 228)
In table.readFromFile (line 41)
In readtable (line 216)
t =
10×9 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9
____ ______ ______ ______ __________ ______ ____ ____ ____
27 76.621 136.77 13.367 02/01/2017 7.4455 1400 1500 27
31 70.869 146.91 19.122 02/01/2017 8.2649 900 1000 31
33 66.703 157.68 23.291 02/01/2017 9.0521 800 900 33
36 64.248 168.63 25.743 02/01/2017 9.7954 600 700 36
35 63.414 179.4 26.594 02/01/2017 10.498 600 700 35
36 64.096 190.3 25.906 02/01/2017 11.207 600 700 36
35 66.189 200.56 23.805 02/01/2017 11.898 600 700 35
33 69.704 210.54 20.302 02/01/2017 12.614 800 900 33
27 74.781 220.35 15.223 02/01/2017 13.385 1500 1700 22
2 82.11 230.67 7.9063 02/01/2017 14.292 5000 5100 2
You get a table with 10 rows, and 9 variables (columns).
edit: However, I've just noticed that it missed the first row of number. Easily fixed:
>> t = readtable('001.txt', 'HeaderLines', 1, 'ReadVariableNames', false)
Warning: The DATETIME data was created using format 'MM/dd/uuuu' but also matched 'dd/MM/uuuu'.
To avoid ambiguity, use a format character vector. e.g. '%{MM/dd/uuuu}D'
> In table/readTextFile>textscanReadData (line 557)
In table/readTextFile (line 228)
In table.readFromFile (line 41)
In readtable (line 216)
t =
11×9 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9
____ ______ ______ ______ __________ ______ ____ ____ ____
1 84.167 126.79 5.8438 02/01/2017 6.5485 6000 6000 0
27 76.621 136.77 13.367 02/01/2017 7.4455 1400 1500 27
31 70.869 146.91 19.122 02/01/2017 8.2649 900 1000 31
33 66.703 157.68 23.291 02/01/2017 9.0521 800 900 33
36 64.248 168.63 25.743 02/01/2017 9.7954 600 700 36
35 63.414 179.4 26.594 02/01/2017 10.498 600 700 35
36 64.096 190.3 25.906 02/01/2017 11.207 600 700 36
35 66.189 200.56 23.805 02/01/2017 11.898 600 700 35
33 69.704 210.54 20.302 02/01/2017 12.614 800 900 33
27 74.781 220.35 15.223 02/01/2017 13.385 1500 1700 22
2 82.11 230.67 7.9063 02/01/2017 14.292 5000 5100 2

Connectez-vous pour commenter.

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by