Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

why fgetl read spaces?

Asked by huda nawaf on 1 May 2012

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

huda nawaf

Tags

Products

No products are associated with this question.

3 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.

[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

Geoff
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

Image Analyst on 1 May 2012

Or John's nice "allwords": http://www.mathworks.com/matlabcentral/fileexchange/27184-allwords or t=str2num(oneLineOfText)

Walter Roberson
Answer by Elige Grant 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

Daniel on 1 May 2012

@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 on 1 May 2012

what if txt file just numbers?

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

Elige Grant

Contact us