MATLAB: Why fgetl read spaces

fgetl

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

Best Answer

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