Filter löschen
Filter löschen

How to read data from text file upto some point defined by "1"?

1 Ansicht (letzte 30 Tage)
Rinu
Rinu am 30 Nov. 2014
Kommentiert: Rinu am 1 Dez. 2014
I'm trying to read data from a number of text files (using a loop) using fscanf upto some point in the file where "1" occurs. I cannot set the "size" in fscanf(fid,'%f',size) because the "1" doesn't occur at the same place in each file. Is there a way to tell Matlab to scan only upto the "1" by something like this (it helps because no data point is equal to "1"):
while fscanf(fid,'%f',1)~=1
scan=fscanf(fid,'%f')
end
  3 Kommentare
Rinu
Rinu am 30 Nov. 2014
Thanks, I'll try that.
dpb
dpb am 30 Nov. 2014
What's the form of the text? Is the objective the first character '1' and is it important to be that precise character or the line containing the character or what? What's the desired result?
So many questions, so few answers/details... :)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 30 Nov. 2014
Assuming your file is just numbers and spaces, and that you want to stop at the first whole number equal to one (and not the 1 in 30.102 for example), no, there isn't a simpler way than using fscanf as in your example. Your while is a bit wonky though:
n = fscanf(fid, '%f', 1);
while ~isempty(n) && n~=1
scan = [scan n];
n = fscanf(fid, '%f', 1);
end
However, it may be more efficient / faster to just read the whole file and truncate the returned array at the first 1:
scan = fscanf(fid, '%f')
pos = find(scan == 1, 1);
scan(pos:end) = [];

Weitere Antworten (0)

Kategorien

Mehr zu Data Import and Export finden Sie in Help 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