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: fscanf and newline - comp.soft-sys.matlabHello, 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 da... Howto read newline character - comp.lang.java.helpfscanf and newline - comp.soft-sys.matlab Howto read newline character - comp.lang.java.help how to write ASCII data using FWRITE function - comp.soft-sys ... if an EOF ... exit a script, but don't exit matlab - comp.soft-sys.matlab ...fscanf and newline - comp.soft-sys.matlab... fgetl(fid); mystring2 = fgetl(fid); but i don't like ... as myvalue = fscanf(fid, '%f\n', 1); (2) Leave ... Arena - Reading Formatted Input from File - comp.simulation ...fscanf and newline - comp.soft-sys.matlab Hello, Suppose I have a text file containing the ... and it seemed like it didn't work, but after reading ... category of FAQ) is ... Shortest way to read all lines (one by one) from a text file ...fscanf and newline - comp.soft-sys.matlab Shortest way to read all lines (one by one) from a text file ... fscanf and newline - comp.soft-sys.matlab Shortest way to read ... How to skip lines when reading in a file - comp.lang.idl-pvwave ...fscanf and newline - comp.soft-sys.matlab... have a text file containing the two lines foobar1 1.234 foobar2 Then after opening the file, i can try this to read ... don't ... Only number input thru scanf() - comp.lang.cgrade.c is an example of using fscanf to input strings of numeric data: http ... one character long (which it will be since any character other than a single newline ... fscanf - C++ Reference - cplusplus.com - The C++ Resources Network... Any character that is not either a whitespace character (blank, newline or tab) or ... expected to be references (pointers): if you want to store the result of a fscanf ... fscanf and newline - comp.soft-sys.matlab | Computer GroupHello, 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 da... 7/22/2012 11:47:35 PM
|