why fgetl read spaces?
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (3)
Dr. Seis
am 1 Mai 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 Kommentare
Walter Roberson
am 1 Mai 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
am 1 Mai 2012
Walter Roberson
am 1 Mai 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
am 1 Mai 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
am 1 Mai 2012
Walter Roberson
am 1 Mai 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
am 1 Mai 2012
0 Stimmen
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 Kommentar
Image Analyst
am 1 Mai 2012
Or John's nice "allwords": http://www.mathworks.com/matlabcentral/fileexchange/27184-allwords or t=str2num(oneLineOfText)
Martin Alexandersson
am 16 Mai 2015
0 Stimmen
fgets instead of fgetl seems to be what you're looking for...
1 Kommentar
Walter Roberson
am 16 Mai 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!
Kategorien
Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!