A blog to help electrical engineers and students to learn electrical engineering topics. Basic Electrical Engineering, Power Systems, Electrical Machines, tec.. Now special course on MATLAB for Beginners.

Class

Class

Saturday, June 4, 2016

Working with Files in MATLAB - Part 4

fseek( ) - Set file position indicator

fseek is the function used to set the position indicator to the byte with the specified Offset relative to the given Origin.
S = fseek(FID, Offset, Origin) relocate the file position pointer in the file associated with the given FID.
Offset value may be positive or negative integers. If the value is positive the pointer will be moved after the Origin and if the value is negative the pointer will be mover before the Origin.
Origin may be starting of the file ('bof' or -1), end of the file ('eof' or  1) or current pointer position ('cof' or  0).  
Example:
>> fileID = fopen('test.txt','rt');
>> A =fseek(fileID, 7, -1)
Relocate the file position pointer to the point after the text Welcome in the file.
>> fseek(fileID, 0, -1)
rewinds the file.

frewind( ) -  Rewind the file

frewind(FID) sets the file position indicator to the beginning of the file.

ftell( ) - Get file position indicator. 

ftell function is used to get the current position pointer position of the file.
P = ftell(FID) returns the location of the file position indicator in the specified file.  Position is indicated in bytes from the beginning of the file.  P will be a non-negative integer. If -1 is returned, it indicates that the query was unsuccessful. Use FERROR to determine the nature of the error.
>> fseek(fileID, 7, -1);
>> ftell(fileID)
ans =
     7
submit to reddit