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

 Akzeptierte Antwort

jonas
jonas am 18 Aug. 2018

0 Stimmen

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 Kommentare

Luis Paniagua
Luis Paniagua am 19 Aug. 2018
Sorry, but it didn't work, in the third column it changed every value to 0 :/
I don't know if it has something to do, but the last line of the txt file is a # like shown below:
42 38 No Data
43 38 4
44 38 4
45 38 5
#
jonas
jonas am 19 Aug. 2018
Bearbeitet: jonas am 19 Aug. 2018
I cannot reproduce your result. Can you please do either one of the following
  • Test the code with the data you provided (attached to this comment)
  • Upload the entire text-file
Luis Paniagua
Luis Paniagua am 19 Aug. 2018
I tried with the one that you sent and it worked, but not with mine, here it is.
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 am 19 Aug. 2018
It worked, thank you so much :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Import and Analysis finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by