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
Example of Nested functions: For illustration, the function below computes and returns the volume of a cube. Three arguments are then passed to it, for the width and length of

Illustration of reading from files: For illustration, suppose that there is a data file 'subjexp.dat' that has on each line a number followed by thecharacter code. The type fu

Illustration of Nested loops: Running the script shows the output: >> printstars ***** ***** ***** The variable rows identifies the number of rows to print, and

Example of sorting strings: To sort a cell array of strings, also the sort function is used. The illustration is as follows, >> engcellnames = {'Chemical','Mechanical',.

Polynomials: Simple curves are polynomials of various degrees, or orders. The degree is the integer of the highest exponent in the expression. The illustrations are as follows

Logical errors: The Logical errors are more complicated to locate, as they do not result in any error message. The logical error is a mistake in reasoning by the programmer, b

Variable packages: Variable packages are now the vector of structures, hence each and every element in the vector is a structure. To show one element in the vector, an index i

Sequential Search: A sequential search is completed by looping through the vector element-by-element starting from the beginning, looking for the key. Usually the index of the

Illustration of Median When the vector is not in sorted order to start with, the median function will still return the right answer (it will sort the vector automatically). For

Debugging Techniques: Any error in the computer program is called a bug. This word is thought to date back to the 1940s, whenever a problem with an early computer was found to