Filter löschen
Filter löschen

Using textscan to read in from a common format .txt file

6 Ansichten (letzte 30 Tage)
Gordon Jones
Gordon Jones am 22 Jun. 2011
I have a number of .txt files that have been produced for a project I am working on. They include within them some data that I need to extract into MATLAB for plotting purposes, however every time I try I get empty arrays with no data included at all.
The data is formatted as follows;
WindSpeed Kts
20
1.0000
18.00
and the script that I am trying to use is;
fid = fopen(fname,'r');
tline = fgetl(fid);
sdepth = textscan(fid,'WindSpeed Kts %f',2);
sdepth = sdepth{1};
But, as stated, all this gives me is an empty array. What I'm trying to get is the first line of the numbers following Kts.

Akzeptierte Antwort

Gordon Jones
Gordon Jones am 25 Jun. 2011
Unfortunately none of these ideas worked, but thanks for suggesting them. However, I managed to achieve the desired result by a somewhat less elegant method.
allstr = textscan(fid,'%s', 'Delimiter', '\n');
allstr = allstr{1};
expend = strncmp(allstr,'WindSpeed Kts',13);
rowexp=find(expend);
sdepth = allstr(rowexp+1);
sdepth = sdepth{1};
this allowed the process to go through multiple different files to find the data I wanted and could be easily modified to find different datastrings. Thanks for all the ideas though, I'm sure if I understood what textscan was doing better your suggestions would all have been excellent.

Weitere Antworten (3)

Walter Roberson
Walter Roberson am 22 Jun. 2011
Your line tline = fgetl(fid); reads in the first line, and you do not do anything with that text so it is discarded. Then you try to use textscan() to continue from that position and to try to read in the very string that appeared on the first line but which is no longer in the buffer because you already read and discarded it...

B. J.
B. J. am 22 Jun. 2011
Try something like this:
while tline ~= -1
if feof(fid) == 1
break
else
tline = fgetl(fid); % Read in the first line only (text headers)
commaLocs = strfind(tline,','); % Finds the commas
start=1;
for colIdx=1:length(commaLocs)
data_cell{n,colIdx}=tline(start:commaLocs(colIdx)-1);
start=commaLocs(colIdx)+1;
end
data_cell{n,colIdx+1} = tline(start:end);
n = n+1;clear start
end
end
  1 Kommentar
Gordon Jones
Gordon Jones am 23 Jun. 2011
I'm not sure I understand this one, I don't have a comma-seperated dataset, so searching for commas won't help me.

Melden Sie sich an, um zu kommentieren.


Kelly Kearney
Kelly Kearney am 22 Jun. 2011
With the code above, you're telling Matlab to read one line (with fgetl) and throw it away. Then you resume reading the file but look for the string literal header with textscan, but you've already passed that point in the file, so textscan stops without reading anything.
Try this instead:
fid = fopen(fname, 'r');
sdepth = textscan(fid, '%f', 'headerlines', 1);
fclose(fid);
  3 Kommentare
Walter Roberson
Walter Roberson am 23 Jun. 2011
That code will extract floating point numbers until it encounters end of file or something that it cannot interpret as a floating point number.
If you want to extract a single floating point number,
sdepth = textscan(fid, '%f', 1, 'headerlines', 1);
Kelly Kearney
Kelly Kearney am 23 Jun. 2011
Assuming your file is actually formatted like your example, it should read all the numerical data in the file. With textscan, if you don't specify the exact number of things to read, then it reads as far as it can with the format specifier you gave it. In this case, I'm telling it to skip one header line, then start reading numbers until it reaches the end of the file (or something non-numerical).

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by