reading a file with fscanf returns too many rows

Hi,
I'm reading a text file using fscanf that has the following format:
32 (number of columns - 5, the number of columns is 37)
448 (number of data rows)
448 data rows with 37 columns
I'm using the following simple function:
function [feat nb dim]=loadFeatures(file)
fid = fopen(file, 'r');
dim=fscanf(fid, '%f',1);
if dim==1
dim=0;
end
nb=fscanf(fid, '%d',1);
% feat = fscanf(fid, '%f', [5+dim, inf]);
feat = fscanf(fid, '%f', [5+dim, inf]);
fclose(fid);
end
Somehow, the returned "feat" is of size 37X3161? How can that be?
Any help will be highly valued as I'm quite stuck with this problem.
Thank you,
Gil

8 Kommentare

kjetil87
kjetil87 am 26 Jul. 2013
The code looks fine , seems to work on a small example aswell. What happens if you check the contents of feat? e.g does the 448 first elements of each column match the content of you file?
If so there could be some rubbish in the file?
dpb
dpb am 26 Jul. 2013
Needs must see portion of file rather than just a description thereof. If it were precisely what you think it wouldn't be a mystery, would it? :)
Gil
Gil am 26 Jul. 2013
Thanks for the answer.
Well, the first 37 elements of feat1 are ok, but then I have about 6 columns of 37 elements + 2 elements that are 0 or 1. the elements after that are ok and then it happens again.
I should mention that columns 6-37 of the file contain only 0 or 1.
I there some way to attach the file to the question? perhaps there is something wrong with the file.
Gil
Gil am 26 Jul. 2013
Here are the first 4 lines of the file:
32
448 731.000000 37.750000 0.100000 0.000000 0.100000 0 1 1 0 0 0 1 0 0 1 1 0 0 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0 0 1 0 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 1 0 1 1 1 0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 0 0 1 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 1 1 0 0 0 1 0 1 1 1 1 0 0 0 1 0 1 0 1 1 1 1 0 1 0 1 1 1 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 1 0 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 1 0 0 0 1 1 1 0 0 0 0 1 0 1 0 0 0 0 1 1 0 0 1 1 1 0 1 0 1 1 0 0 1 1 0 1 1 1 0 0 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 0 1 0 1 1 0 0 1 1 0 0 0 0 1 0 0 0 1 1 1 1 1 0 0
716.000000 43.000000 0.100000 0.000000 0.100000 0 0 0 1 1 0 1 0 0 0 0 0 0 1 0 1 0 0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 0 0 1 0 0 0 0 1 1 0 0 1 1 0 1 1 1 0 1 0 0 0 0 1 1 1 0 0 1 0 0 1 1 1 0 1 0 1 0 1 0 1 1 1 0 1 1 0 1 1 1 0 1 0 1 0 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 1 1 1 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 1 1 1 1 1 1 1 0 0 1 0 0 1 0 1 0 0 1 0 1 1 0 0 0 0 0 1 1 0 0 1 1 0 1 0 1 0 0 1 1 1 1 0 1 1 1 1 1 1 1 0 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 1 1 1 1 1 1 0 0 1 0 1 1 0 0 0 1 0 1 1 0 0 0 1 1 0 0 1 1 1 1 0 0 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 1 1 0 1 1 1 1
Cedric
Cedric am 26 Jul. 2013
Bearbeitet: Cedric am 26 Jul. 2013
There are way more than 37 columns in the data that you provided (261 in fact), so what you observe is a reshape of them read linearly into an array with 37 columns.
dpb
dpb am 26 Jul. 2013
Bearbeitet: dpb am 26 Jul. 2013
>> r='448 731.000000 37.750000 0.100000 0.000000 0.100000 0 1 1 0 0 0 1 0 0 1 1 0 0 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0 0 1 0 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 1 0 1 1 1 0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 0 0 1 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 1 1 0 0 0 1 0 1 1 1 1 0 0 0 1 0 1 0 1 1 1 1 0 1 0 1 1 1 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 1 0 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 1 0 0 0 1 1 1 0 0 0 0 1 0 1 0 0 0 0 1 1 0 0 1 1 1 0 1 0 1 1 0 0 1 1 0 1 1 1 0 0 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 0 1 0 1 1 0 0 1 1 0 0 0 0 1 0 0 0 1 1 1 1 1 0 0';
>> n=str2num(r);
>> length(n)
ans =
262
>>
In fact, there are 262 numeric columns in the first row; not sure about second...oh, the first record has the '448' pasted in front so in reality there are probably 261 in each record. That seems to match precisely what you have gotten.
Looks like your assumption of what the 32 means is in error...
Gil
Gil am 26 Jul. 2013
Thanks Cedric Wannaz and dpb, I can't believe I missed it!
Cedric
Cedric am 26 Jul. 2013
Well, let me reassure you by saying that I spent a good 5 minutes looking for a coding issue, before I finally decided to count columns!

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

Gil
am 26 Jul. 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by