help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Why is my data not being read correctly?


From: Terry Duell
Subject: Why is my data not being read correctly?
Date: Thu, 07 Feb 2013 14:01:19 +1100
User-agent: Opera Mail/12.13 (Linux)

Hello All,
Perhaps I should rephrase the subject line...why is my data not being read as I expect it to be? I am using fscanf to read data from a file. The problem is that once I have read the first 3 lines (header), the first data entry appears to be skipped.
The header appears to read OK, as the the correct number of reads is known.
The following script displays the issue.

%read data file
fp1 = fopen("test_data.txt","r")
name1 = fscanf(fp1,"%s \n","C")
junk1 = fgets(fp1,128) % use fgets with a max, then no need for str len
[num1 spac1] = fscanf(fp1,"%6f%6f\n","C")
dat = zeros(2,num1);
% check what we have
name1
num1, spac1
% read
for i = 1:num1
        dat(1,i) = (i-1)*spac1;
        dat(2,i) = fscanf(fp1,"%f \n","C");
end
fclose(fp1);
dat

The data file ('test_data.txt') has been doctored, with an extra data item added at the end to prevent an error. The data file is follows...

NAME
Descriptor stuff
 9 7.55
   59.80
   59.80
   60.51
   59.80
   59.80
   59.37
   59.06
   58.94
   59.02
   44.11

and the response I get here from Octave 3.6.3 is as follows...

fp1 =  3
name1 = NAME
junk1 = Descriptor stuff

num1 =  9
spac1 =  7.5500
name1 = NAME
num1 =  9
spac1 =  7.5500
dat =

0.00000 7.55000 15.10000 22.65000 30.20000 37.75000 45.30000 52.85000 60.40000 59.80000 60.51000 59.80000 59.80000 59.37000 59.06000 58.94000 59.02000 44.11000

I'm not very familiar with C, so it is possible that my method of reading the data is flawed, but thus far I haven't been able to see why the script is behaving as it is.

Can anyone see why the first data item (the first 59.80) isn't being read?

Cheers,
--
Regards,
Terry Duell


reply via email to

[Prev in Thread] Current Thread [Next in Thread]