fscanf and newline

  • Follow


Hello,

Suppose I have a text file containing the two lines

foobar1
1.234
foobar2

Then after opening the file, i can try this to read the data:

mystring1 = fgetl(fid);
myvalue   = fscanf(fid, '%f', 1);
mystring2 = fgetl(fid);

However, this doesn't work since the fscanf call doesn't read the
newline after 1.234.  I can solve this with

mystring1 = fgetl(fid);
myvalue   = fscanf(fid, '%f', 1);
fgetl(fid);
mystring2 = fgetl(fid);

but i don't like this 'hack'-solution.  Is there a way to modify
my call to fscanf so that I don't need to add the extra fgetl to
skip the newline?

Thanks!
Bart

-- 
	"Share what you know.  Learn what you don't."
0
Reply Bart 10/18/2010 11:22:48 AM

> Suppose I have a text file containing the two lines
>
> foobar1
> 1.234
> foobar2
>
> Then after opening the file, i can try this to read the data:
>
> mystring1 = fgetl(fid);
> myvalue   = fscanf(fid, '%f', 1);
> mystring2 = fgetl(fid);
>
> Is there a way to modify
> my call to fscanf so that I don't need to add the extra fgetl to
> skip the newline?
>

A couple of possible solutions:

(1)  Tell fscanf to read the newline (\n), such as

myvalue   = fscanf(fid, '%f\n', 1);

(2) Leave off the "1", and let fscanf decide that there is only one number, 
such as

myvalue   = fscanf(fid, '%f');
 

0
Reply Leslie_McBrayer 10/18/2010 12:23:59 PM


On 2010-10-18, Leslie_McBrayer <lmcbraye@mathworks.com> wrote:
>>
> A couple of possible solutions:
>
> (1)  Tell fscanf to read the newline (\n), such as
>
> myvalue   = fscanf(fid, '%f\n', 1);
>
> (2) Leave off the "1", and let fscanf decide that there is only one number, 
> such as
>
> myvalue   = fscanf(fid, '%f');

Hmmm... i thought I had tried that and it seemed like it didn't
work, but after reading your answer and re-trying it seems like
it works now :-)

However, what if the line that I'm fscanf'ing contains more than
one (say N with N a non-fixed integer) floating point value?  It seems like

  myvalue   = fscanf(fid, '%f\n', N);

or

  myvalue   = fscanf(fid, '%f', N);

does not work.  I still have to somehow 'dummy read' the trailing
newline with an extra fgetl statement...

Regards,
Bart

-- 
	"Share what you know.  Learn what you don't."
0
Reply Bart 10/18/2010 1:59:39 PM

Bart Vandewoestyne wrote:
....

> However, what if the line that I'm fscanf'ing contains more than
> one (say N with N a non-fixed integer) floating point value?  It seems like
> 
>   myvalue   = fscanf(fid, '%f\n', N);
> 
> or
> 
>   myvalue   = fscanf(fid, '%f', N);
> 
> does not work.  I still have to somehow 'dummy read' the trailing
> newline with an extra fgetl statement...
....

Indeed, the colossal pita (and a candidate for highest category of FAQ) 
is the way C works w/ formatted input (and hence carried over into 
Matlab)... :(

fmt = [repmat(1,N,'%f') '\n'];
ary = fscanf(fid, fmt);

--
0
Reply dpb 10/18/2010 2:14:32 PM

Dear Bart,

> dpb:
> fmt = [repmat(1,N,'%f') '\n'];

He meant: 
  fmt = [repmat('%f, 1, N) '\n'];

Another way is reading the line at first with FGETL and parse it afterwards by SSCANF.

Good luck, Jan
0
Reply Jan 10/18/2010 8:39:03 PM

4 Replies
381 Views

(page loaded in 0.046 seconds)

Similiar Articles:










7/22/2012 11:47:35 PM


Reply: