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

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

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];

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

Introduction and Purpose In the lectures and tutorials you have been introduced to a number of probability distributions. You have also learned that modelling any system (such

Index vector: The index vector is then used as the indices for original vector. To get grades vector in ascending order, the indices used would be grades (2), grades (5), and

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

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 suc

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

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