# Can anyone tell me how to use fseek in the matlab

Status
Not open for further replies.

#### AILab

##### Newbie level 5
Hi, i wanted to used the fseek to do the looping in the matlab but, when i use fseek in the matlab, i found that the dataset that fseek point is not correct when i debug the matlab, can anyone tell me how actually fseek working because i try to search online but could not get any useful information.Thanks

#### blooz

##### Advanced Member level 2
FSEEK Set file position indicator.
STATUS = FSEEK(FID, OFFSET, ORIGIN) repositions the file position
indicator in the file associated with the given FID. FSEEK sets the
position indicator to the byte with the specified OFFSET relative to
ORIGIN.

FID is an integer file identifier obtained from FOPEN.

OFFSET values are interpreted as follows:
>= 0 Move position indicator OFFSET bytes after ORIGIN.
< 0 Move position indicator OFFSET bytes before ORIGIN.

ORIGIN values are interpreted as follows:
'bof' or -1 Beginning of file
'cof' or 0 Current position in file
'eof' or 1 End of file

eg
fseek(fid,0,-1)

Code:
%blooz fseek Example
clc
fid = fopen('sample.txt', 'w');%Opens a New files to Write
A=uint32(rand(1,10)*20);% Some random integers and convert to unsigned integer 32bit
A' %print to screen
fwrite(fid, A, 'uint32');%Write them to file
fclose(fid);%close file

fid = fopen('sample.txt', 'r');%Again open in read mode
fseek(fid, 4, 'bof');%seek to the 4th byte
A = fread(fid,5, 'uint32');%then read 5 unsignedinteger 32bit
A %Show Values
fclose(fid);%close file
output
Code:
ans =

3
0
18
4
6
13
6
9
1
20

A =

0
18
4
6
13

navenmou

### navenmou

points: 2

#### AILab

##### Newbie level 5
currently i am trying to divide my data to the 30 training data from the data of 31 until 60 as training set and 61 to 90 as testing set and this loop will continue until the end of the data, here is my coding

for i=2:floor(((Train.total-IniTrain.data)/L)-1);
fseek(fid,(i+(L+7.6))*L ,'bof'); % go to the first data of a chunk
AddTrain.data = fscanf(fid, '%g', [Train.dim+1,L]);
% Read Test Data
fseek(fid,(i+(L+46.35))*L,'bof');
Test.dim = Train.dim;
Test.data = fscanf(fid, '%g', [Test.dim+1,L]);
Test.class = Test.data(Test.dim+1,;
Test.data = Test.data (1:Test.dim,;

but, when i so the debug, i found that the loop will not go to the second i and the results are totally wrong. besides, when i try to check the data when i change i=2, the dataset is not start from the 61, can anyone tell me what wrong with my coding? Thanks

#### blooz

##### Advanced Member level 2
currently i am trying to divide my data to the 30 training data from the data of 31 until 60 as training set and 61 to 90 as testing set and this loop will continue until the end of the data, here is my coding

for i=2:floor(((Train.total-IniTrain.data)/L)-1);
fseek(fid,(i+(L+7.6))*L ,'bof'); % go to the first data of a chunk
AddTrain.data = fscanf(fid, '%g', [Train.dim+1,L]);
% Read Test Data
fseek(fid,(i+(L+46.35))*L,'bof');
Test.dim = Train.dim;
Test.data = fscanf(fid, '%g', [Test.dim+1,L]);
Test.class = Test.data(Test.dim+1,;
Test.data = Test.data (1:Test.dim,;

but, when i so the debug, i found that the loop will not go to the second i and the results are totally wrong. besides, when i try to check the data when i change i=2, the dataset is not start from the 61, can anyone tell me what wrong with my coding? Thanks
you can document your code with some comments so that ,it will be easier for coders to read and debug ,

the above code doesn't have an end statement ,In matlab for loop must be terminated by an end statement .

#### AILab

##### Newbie level 5
for a=ss:es
fprintf('%2d-st Session\n',a);
for i=1:floor(((Train.total-IniTrain.data)/L)-1);
fseek(fid,(i+(L+7.6))*L ,'bof'); % go to the first data of a chunk
AddTrain.data = fscanf(fid, '%g', [Train.dim+1,L]);
% Read Test Data
fseek(fid,(i+(L+46.35))*L,'bof');
Test.dim = Train.dim;
Test.data = fscanf(fid, '%g', [Test.dim+1,L]);
Test.class = Test.data(Test.dim+1,;
Test.data = Test.data (1:Test.dim,;

proto.num=protoN;
else
proto.num = protoN;
proto.data = AddTrain.data ,1rotoN);
proto.class = AddTrain.class ,1rotoN);
proto.dim = Train.dim;
end

fprintf('------ Start CIPCA10 ------\n'); % call function for CIPCA
CIPCA10_rec(1,a)= CIPCA10_EIG.Time;
CIPCA10_rec(2,a)= CIPCA10_EIG.dim;
CIPCA10_rec(3,a) = NN(proto,Test,CIPCA10_EIG);

% Computation time display
fprintf('Computation time\n');
fprintf('[CIPCA] %f\n',CIPCA10_rec(1,a));

% Dimensional numeral
fprintf('Number of dimensions\n');
fprintf('[CIPCA] %f\n',CIPCA10_rec(2,a));

% The recognition rate show
fprintf('Recognition rate\n');
fprintf('[CIPCA] %f\n',CIPCA10_rec(3,a));

S = sprintf('%d',a);
fname = ['eigv_NEweather1',S,'.mat'];
save(fname);

Train = [];
Test = [];
end
end

This is the loop but, when i try to debug, i found that the loop didn't go back to the for, but directly go to the fseek. i wonder why?

#### AILab

##### Newbie level 5
Actually i have a dataset of 123 1
456 2
789
10 11 12 1
13 14 15 2
16 17 18 2
19 20 21 2
22 23 24 1
25 26 27 1

with last number is the class level, and actually i want to read 3 line (i line one data) of number for every loop but the problem i need to read the first loop at the line number 4 and until the end.but the problem is after i the first loop the value is correct, but when i try to debug the second loop the value is always incorrect.

Status
Not open for further replies.