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 polyval - interpolation: The function polyval can then be used to compute the polynomial at particular values. For illustration, we could compute at every value in th

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

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',.

Vectors of nested structures: The Combining vectors and nested structures are possible to have a vector of structures in which several fields are structures themselves. Here i

Curve Fitting: The data is fundamentally either discrete or continuous. In most of the applications, data is sampled, for illustration, The temperature recorded every ho

Reading from Files: There are many lower level functions which read from files. The function fscanf reads the formatted data into a matrix, by using conversion formats like %s

Example of Core objects: The one core graphics object is a line that is also what the plot function produces. Here is an illustration of generating a line object, altering som

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

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

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