Using textscan to read in from a common format .txt file
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (3)
Walter Roberson
le 22 Juin 2011
1 vote
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.
le 22 Juin 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 commentaire
Gordon Jones
le 23 Juin 2011
Kelly Kearney
le 22 Juin 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 commentaires
Gordon Jones
le 23 Juin 2011
Walter Roberson
le 23 Juin 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
le 23 Juin 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).
Catégories
En savoir plus sur Text Files dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!