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 で Large Files and Big Data についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!