why fgetl read spaces?
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (3)
Dr. Seis
el 1 de Mayo de 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 comentarios
Walter Roberson
el 1 de Mayo de 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
el 1 de Mayo de 2012
Walter Roberson
el 1 de Mayo de 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
el 1 de Mayo de 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
el 1 de Mayo de 2012
Walter Roberson
el 1 de Mayo de 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
el 1 de Mayo de 2012
0 votos
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 comentario
Image Analyst
el 1 de Mayo de 2012
Or John's nice "allwords": http://www.mathworks.com/matlabcentral/fileexchange/27184-allwords or t=str2num(oneLineOfText)
Martin Alexandersson
el 16 de Mayo de 2015
0 votos
fgets instead of fgetl seems to be what you're looking for...
1 comentario
Walter Roberson
el 16 de Mayo de 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!
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!