Skip big header from a txt file

2 views (last 30 days)
Luis Paniagua
Luis Paniagua on 18 Aug 2018
Commented: Luis Paniagua on 19 Aug 2018
I would like to extract the data only from a text file. the text file has a header (the first 11 lines) which contains some numerical values as shown below, also the data is separated in three columns, one of these columns, has numerical values, but also the written characters: "No Data". I also would like to change that "No Data" for the numerical value 0
1 9 1 6 "Thu Feb 13 13:12:30 2014 "
0 0 0 0 0 0
38 38 915 915
"CJE "
"2 "
"110321-025-01D-1ST
0 0 1 .1 73.7972 17 50
1 0 7 1 60 0 0 0 0
0 " "
1 0
#
38 38 No Data
39 38 No Data
40 38 No Data
41 38 3
42 38 No Data
43 38 4
44 38 4
45 38 5

Accepted Answer

jonas
jonas on 18 Aug 2018
fid = fopen('datam.txt');
C = textscan(fid,'%d%d%d%*[^\n]',...
'Delimiter',' ',...
'TreatAsEmpty','No',...
'MultipleDelimsAsOne',true,...
'headerLines', 11)
fclose(fid)
cell2mat(C)
ans =
8×3 int32 matrix
38 38 0
39 38 0
40 38 0
41 38 3
42 38 0
43 38 4
44 38 4
45 38 5
  6 Comments
jonas
jonas on 19 Aug 2018
ah yea, those in the 3rd column are floats. Better read everything as floats instead:
fid = fopen('datam2.txt');
C = textscan(fid,'%f%f%f%*[^\n]',...
'Delimiter',' ',...
'TreatAsEmpty','No',...
'MultipleDelimsAsOne',true,...
'headerLines', 11,...
'EmptyValue',0)
fclose(fid)
C=cell2mat(C);
Luis Paniagua
Luis Paniagua on 19 Aug 2018
It worked, thank you so much :)

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!