why fgetl read spaces?
Mostra commenti meno recenti
hi,
are there any by which fgetl do not read space.
i.e
s=fopen('d:\matlab\r2011a\bin\flixster_time\ratings_tf\rate.txt');
for k=1:2
t=fgetl(s);end;
t will be:
1 4 3 7 2 3 4 90 12 7 8 3 4
when need t(1), i will get 1
but when need t(2) get
get space
what i have to do to get 4 when need t(2)?
thanks
Risposta accettata
Più risposte (3)
Dr. Seis
il 1 Mag 2012
If they are all just numbers on the line, then converted the string to numeric should do the trick
new_t = str2num(t);
6 Commenti
Walter Roberson
il 1 Mag 2012
Caution: str2num() eval()'s its input, so if there happens to be any text it will be interpreted as a command or function call.
huda nawaf
il 1 Mag 2012
Walter Roberson
il 1 Mag 2012
str2num() solves your problem until the first time that the program attempts to read a file that happens to contain the line (e.g.)
!del *.*
Daniel Shub
il 1 Mag 2012
@Walter, I was thinking of posting the same comment, but given huda's experience with MATLAB, I was a little concerned. I think 'quit' is a better and safer example or maybe !shutdown
huda nawaf
il 1 Mag 2012
Walter Roberson
il 1 Mag 2012
MATLAB does not support the operating system facilities necessary to be *certain* that a file is just numbers. Be safe instead. For example,
str2double(regexp(t, ' ', 'split'))
str2double() does *not* use eval().
Walter Roberson
il 1 Mag 2012
0 voti
fgetl() is defined to read a line at a time. The "l" at the end of the name stands for "line". There is no way to change that. You will need to use a different input function or a different way of parsing the string you read in. For example, regexp(t, ' ', 'split')
1 Commento
Image Analyst
il 1 Mag 2012
Or John's nice "allwords": http://www.mathworks.com/matlabcentral/fileexchange/27184-allwords or t=str2num(oneLineOfText)
Martin Alexandersson
il 16 Mag 2015
0 voti
fgets instead of fgetl seems to be what you're looking for...
1 Commento
Walter Roberson
il 16 Mag 2015
No, not at all. The difference between fgets() and fgetl() is that fgetl() removes the line terminator characters and fgets() does not. Otherwise they are exactly the same, both of them returning character strings. The original poster was trying to index the resulting string by groups of numbers rather than by characters. The original poster also wanted the binary integer values corresponding to interpreting the characters, such as wanting the number 4 when the character '4' was read. fgets() does none of this!
Categorie
Scopri di più su Large Files and Big Data in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!