Opening and closing a file, MATLAB in Statistics

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 string is used to identify which mode (example, writing or appending). The fopen function returns 1 when it is not successful in opening a file, or an integer value, that becomes the file identifier when it is successful. The file identifier is then used to refer the file whenever calling other file I/O functions. The common form is as shown below:

fid = fopen('filename', 'permission string');

The permission strings include:

r reading (this is the default)

w writing

a appending

View helps fopen for others.

 

Afterward the fopen is tried; the value returned must be tested to make sure that the file was efficiently opened. For illustration, if the files do not exist, the fopen will not be successful. As the fopen function returns -1 when the file was not found, this can be checked to decide whether to print an error message or to continue and use the file. For illustration, if it is preferred to read from a file 'samp.

dat':

fid = fopen('samp.dat');

if fid == -1

   disp('File open not successful')

else

   % Carry on and use the file!

end

 

The Files must be closed whenever the program has finished the reading from or writing to them. The function which accomplishes this is the fclose function, that returns 0 if the file close was successful, or -1 if not. The Individual files can be closed by identifying the file identifier, or if more than one file is open, then all the open files can be closed by passing the string 'all' to the fclose function. The common forms are as shown below:

closeresult = fclose(fid);

closeresult = fclose('all');

 

This must also be checked with an if-else statement to make sure that it was successful.

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







Related Discussions:- Opening and closing a file, Assignment Help, Ask Question on Opening and closing a file, Get Answer, Expert's Help, Opening and closing a file Discussions

Write discussion on Opening and closing a file
Your posts are moderated
Related Questions
Types of errors: There are some various kinds of errors which can occur in a program, that fall into the categories of run-time errors, syntax errors, and logical errors. T

Tracing: Sometimes, when a program has loops and/or selection statements and is not running properly, it is helpful in the debugging process to know exactly which statements h

Use of function polyval: The better the curve fit, the more exact these interpolated and extrapolated values will be. By using the subplot function, we can loop to display the

Persistent Variables: Generally, whenever a function stops executing, then the local variables from that function are cleared. That means that each and every time a function i

fopen function - file function: The fopen opens the file for reading. The fscanf then reads each line one double and one character, and put each pair in individual columns in

Graphics objects: The objects involve graphics primitives like lines and text, and also the axes used to orient the objects. These objects are organized hierarchically, and the

Example of customizing plots: As the other illustration of customizing plots, the pieces of a pie chart can be "exploded" from the rest. In this situation, the two vectors are

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

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

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