why fgetl read spaces?
이전 댓글 표시
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
채택된 답변
추가 답변 (3개)
Dr. Seis
2012년 5월 1일
If they are all just numbers on the line, then converted the string to numeric should do the trick
new_t = str2num(t);
댓글 수: 6
Walter Roberson
2012년 5월 1일
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
2012년 5월 1일
Walter Roberson
2012년 5월 1일
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
2012년 5월 1일
@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
2012년 5월 1일
Walter Roberson
2012년 5월 1일
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
2012년 5월 1일
0 개 추천
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
Image Analyst
2012년 5월 1일
Or John's nice "allwords": http://www.mathworks.com/matlabcentral/fileexchange/27184-allwords or t=str2num(oneLineOfText)
Martin Alexandersson
2015년 5월 16일
0 개 추천
fgets instead of fgetl seems to be what you're looking for...
댓글 수: 1
Walter Roberson
2015년 5월 16일
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!
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!