Opening and closing a file, MATLAB in Statistics

Assignment Help:

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.


Related Discussions:- Opening and closing a file

Vectors of nested structures, Vectors of nested structures: The Combin...

Vectors of nested structures: The Combining vectors and nested structures are possible to have a vector of structures in which several fields are structures themselves. Here i

Types of errors, Types of errors: There are some various kinds of erro...

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

Assignment , Determine sequence weights for the sequences ACTA, ACTT, CGTT,...

Determine sequence weights for the sequences ACTA, ACTT, CGTT, and AGAT in problem 1 by using Thompson, Higgins, and Gibson method a) compute pairwise distances between sequences

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

Representing logical true and false, Representing Logical true and False: ...

Representing Logical true and False: It has been stated that expressions which are theoretically true really have the integer value of 1, and expressions which are theoretical

Nested for loops, Nested for Loops: The action of a loop can be any of...

Nested for Loops: The action of a loop can be any of the valid statement(s). Whenever the action of a loop is the other loop, this is known as nested loop. As an illustrati

Sequential search, Sequential Search: A sequential search is completed...

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

Sorting strings, Sorting Strings: For the matrix of strings, the sort ...

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

Illustration of reading from files, Illustration of reading from files: ...

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

Nested functions, Nested Functions: We have seen that the loops can be...

Nested Functions: We have seen that the loops can be nested, that means that one inside of the other, functions can be nested. The terminology for nested functions is that an

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