Generic code for reading from files, MATLAB in Statistics

Generic code for Reading from Files:

The generic code to complete this is as shown below:

 

fid = fopen('filename');

if fid == -1

   disp('File open not successful')

else

   while feof(fid) == 0

     % Read one line into a string variable

     aline = fgetl(fid);

     % Use string functions to extract numbers, strings,

     % etc. from the line

     % Do something with the data!

   end

   closeresult = fclose(fid);

   if closeresult == 0

     disp('File close successful')

   else

     disp('File close not successful')

   end

end

 

The permission string can be involved in the call to the fopen function, for illustration,

fid = fopen('filename', 'r');

 

but is not essential as reading is the default. The condition on the while loop can be interpreted by saying "while the file end-of-file is false." The other way to write this is

while  feof(fid)

that is interpreted as "while we're not at the end of the file."

Posted Date: 10/20/2012 6:20:52 AM | Location : United States







Related Discussions:- Generic code for reading from files, Assignment Help, Ask Question on Generic code for reading from files, Get Answer, Expert's Help, Generic code for reading from files Discussions

Write discussion on Generic code for reading from files
Your posts are moderated
Related Questions
function sortrows - sorting strings: The function sortrows sorts each and every row as a block, or group, and it also will work on numbers. Here in this illustration the rows

Operation on file: We concentrate first on the fgetl function that reads strings from the file one line at a time. The fgetl functions afford more control over how the data is

Opening and Closing a File: The Files are opened with the fopen function. By the default, fopen function opens a file for reading. If the other mode is preferred, a permission

Illustration of Mode When there is more than one value with similar (highest) frequency, then the smaller value is mode. In the case below, as 3 and 8 appear twice in the vecto

Illustration of Input in a for loop: In this illustration, the loop variable iv iterates through the values 1 through 3, therefore the action is repeated three times. The acti

Program of Persistent variables: The program below elaborates this. The script calls a function func1 that initializes a variable count to 0, then increment it, and print the

Nested Structures: The nested structure is a structure in which at least one of the members is itself a structure. For illustration, a structure for the line segment may co

Determine sequence weights for the sequences ACTA, ACTT, CGTT, and AGAT in problem 1 by using Thompson, Higgins, and Gibson method a) compute pairwise distances between sequences

Illustrations of Sequential search: The two illustrations of calling such function is as shown below: >> values = [85 70 100 95 80 91]; >> key = 95; >> seqsearch

Example of Functions which complete a task: For illustration, the function below just prints the number arguments passed to it in a sentence format: As this function i