MATLAB Answers


why fgetl read spaces?

Asked by huda nawaf on 1 May 2012
Latest activity Commented on by Walter Roberson
on 16 May 2015


are there any by which fgetl do not read space.


for k=1:2

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)?



Log in to comment.


4 Answers

Answer by Geoff
on 1 May 2012
 Accepted 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.


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 =

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.


doc sscanf
doc regexp
doc textscan


Log in to comment.

Answer by 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);


@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

what if txt file just numbers?

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().

Log in to comment.

Answer by 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

Or John's nice "allwords": or t=str2num(oneLineOfText)

Log in to comment.

Answer by Martin Alexandersson on 16 May 2015

fgets instead of fgetl seems to be what you're looking for...

  1 Comment

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!

Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Win prizes and improve your MATLAB skills

Play today