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 Mode When no value appears more often than any other, the smallest value in the vector will be the mode of the vector. >> shortx = [2 5 1 4]; >> mode(shortx

Properties of the text box - graphics objects: By using get will show properties of the text box, the illustration is as shown below: >> get(thand)   BackgroundColor

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

Steps for input output functions - Lower level file: The steps involved are as shown below:  Open the file.  Read the file, write to the file, or append to the file.

Example of Median For the vector [1 4 5 9 12 33], the median is the average of the 5 & 9 in the middle: >> median([1 4 5 9 12 33]) ans =     7

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

Logical Built-In Functions: There are built-in functions in the MATLAB which are useful in conjunction with vectors or matrices of all logical true or false values; two of the

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

Vectorizing: In most of the cases in MATLAB, loops are not essential. As MATLAB is written specifically to work with the vectors and matrices, most operations can be completed

Run-time or execution-time error: The Run-time, or execution-time, errors are found whenever a script or function is executing. With most of the languages, an illustration of