why fgetl read spaces?
10 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Geoff
on 1 May 2012
I guess my heart is still in C... I love regexp, but if all I wanted to do was read integers (not strings), I'd do this after reading the line into 't':
t = sscanf(t, '%d');
It's one of those times when MatLab's blasphemous divergence from a universally recognised function is actually useful.
[edit]
Just to clarify WHY you need to do this...
When you read a line of text it is returned as an array of characters (which us computer geeks refer to as a 'string'). When you asked for t(1) you were fooled into thinking it was numbers because your first value only contained a single character.
However, it's not. And you discovered this when you tried to get t(2). The ASCII value for the character '1' is 49, so that happy coincidence of is not so happy after all.
What you need to do is process the string to extract the numbers. Here, they are represented in base-10 form, which is how humans like to write numbers. But that's not how computers like their numbers, so it takes some code to translate.
That's what sscanf does. It churns through a string and pulls out your numbers. I specified the %d format, which means 'integer base-10'. As soon as it encounters a character that doesn't conform to the format (and isn't whitespace), it will stop.
So if all you need to do is read integers separated by whitespace, sscanf does the trick.
If you need something more clever you can use regexp, but you have to be a little careful if your numbers can be negative (you have to actually tell it to allow an optional '-' preceding a number). For example:
> str2num(char(regexp('1 {23} -123, - 66.0', '(-?\d+)', 'match')))
ans =
1
23
-123
66
0
Notice that regexp actually returns strings, so you still need to convert them to numbers (unless you actually want the strings). This function is really good if you need to filter anything that looks like a number out of a string that can contain just about anything else. It's really good for all sorts of other things too. If you do anything with strings, you should learn how to write regular expressions.
Another option: If you expect only some kinds of text 'noise', you can use textscan and state all your known 'noise' characters with the 'delimiter' option.
References
doc sscanf
doc regexp
doc textscan
0 Comments
More Answers (3)
Dr. Seis
on 1 May 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 Comments
Walter Roberson
on 1 May 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
on 1 May 2012
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 Comment
Image Analyst
on 1 May 2012
Or John's nice "allwords": http://www.mathworks.com/matlabcentral/fileexchange/27184-allwords or t=str2num(oneLineOfText)
Martin Alexandersson
on 16 May 2015
fgets instead of fgetl seems to be what you're looking for...
1 Comment
Walter Roberson
on 16 May 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!
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!