Generic code for reading from files, MATLAB in Statistics

Assignment Help:

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."


Related Discussions:- Generic code for reading from files

Function sortrows - sorting strings, function sortrows - sorting strings: ...

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

Mode, Mode The mode of a data set is the value which appears most often...

Mode The mode of a data set is the value which appears most often. The built-in function in a MATLAB for this is known as the mode. >> x = [9 10 10 9 8 7 3 10 9 8 5 10];

Median, Median The median is defined only for a data set which has been...

Median The median is defined only for a data set which has been sorted first, that means that the values are in order. The median of a sorted set of data values (n) is defined

Illustration of binary search, Illustration of Binary search: An illus...

Illustration of Binary search: An illustration to search for the key of 91 in the vector is as shown below: The table below shows what will happen in each iteration of

Properties of the text box - graphics objects, Properties of the text box -...

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

Function polyval - interpolation, Function polyval - interpolation: Th...

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

Illustration of nested for loops and the if statements, Illustration of nes...

Illustration of nested for loops and the if statements: For illustration,when the file contains: 33   -11  2  4      5   9 22     5  -7  2     11    3 the outco

Example of function stubs, Example of Function stubs: The three functi...

Example of Function stubs: The three functions have not so far been written, though, therefore the function stubs are put in place so that the script can be executed and teste

Opening and closing a file, Opening and Closing a File: The Files are ...

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

Advanced file input and output, Advanced File Input and Output: In tha...

Advanced File Input and Output: In that section, we saw how to read the values entered by user using the input as well as the output functions disp and fprintf, that shows inf

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd