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.


fid = fopen('samp.dat');

if fid == -1

   disp('File open not successful')


   % Carry on and use the file!



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
Illustration of Indexing: Though, rather than of creating the index vector manually as shown here, the process to initialize the index vector is use to a sort function. The al

Lower Level File I/O Functions: Whenever reading from data file, as long as the data in the file is "regular" the load function works-in another words, the similar type of dat

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.

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

Illustration of tracing a error: The one way of following the flow of function, or tracing it, is to use the echo function. The echo function, that is a toggle, will show each

Areacirc function: The areacirc function can be called from the Command Window as shown here, or from a script. Here is a script which will prompt the user for the radius of o

Function Stubs: The other common debugging method, which is used when there is a script main program which calls many functions, is to use the function stubs. A function stub

Execution of persistent variables: The functions can also be called from the script or from the Command Window, as shown here. For illustration, the functions are called first

Sorting Strings: For the matrix of strings, the sort function works exactly as formerly for numbers. For illustration, >> words = char('Hi', 'Hello', 'Howdy', 'Goodbye', '

#qYou will need to examine two of the nine sections of data: one section of qualitative data (choose either Gender or Position) one section of quantitative data (choose either Intr