Introduction to Matlab File
In some applications, there is a need to access the text file to do operations like data reading, data writing. MATLAB FILE is used to do different options like write data to a text file or read data from text files, etc. For writing data on text files, we use fprint statements on Matlab. For reading data from a text file, we use a fscanf statement on Matlab. The specifying a type of access mode it would be either read(r) or write (w) we must use fopen statement.
The syntax for Matlab file handling operations is as shown below:-
For Write operation:
For Reading operation:
A = fscanf(fileID,formatSpec)
A = fscanf(fileID,formatSpec,sizeA)
[A,count] = fscanf(___)
How does Matlab File work?
Matlab file is the file handling operations like read a file and write on the file. We see how these operations are work on Matlab.
For witting operations, there is some syntax which we use which are mentioned above. For writing, we take a variable and store some data which we want to write on that file and then we use a fopen statement, and in that, we specify the access type and the text file name and that stored in a file which is nothing but the file identifier. Then we use the writhing statement that is fprintf in that we mentioned the identifier name, data format, and that variable name that stored the data. And then we close that file using the fclose in that file identifier.
For reading operation, we first specify the access type and the file name by using a fopen statement stored in file identifier fileID1. Then we must specify the data format in formatSpec, and then we use fscanf statement to read data from a text file, in fscanf we take file identifier and format specifier, and data which we read from a text file is stored in one variable. Then we simply close the file using a fclose statement.
Examples to Implement Matlab File
Below are examples of Matlab File:
The first example we saw related to writing operations on a file. For that, we create one text file namely ‘textfile1’ with extension .txt. For writing some numbers on that file we can use a ‘rand’ function, basically, and is an inbuilt function available on Matlab this function is used to generate some random numbers. We use rand(5,1) it means the rand function can return five random numbers and multiply by 100 and these five numbers can store in x1 variable. Then we use a fopen statement to specify the file name and which operation we want to perform. For these, we write fopen and in parenthesis take file name (textfile1.txt) and we specify the type of access that is writing operation on file are denoted by ‘w’ these two arguments are separated by a comma and stored in variable fileID1.then using fprintf we can write these random numbers on the text file. And finally, close that file using a fclose statement. Then to verify we use the type function, a type is used to display the contents of the file.
x1 = 100*rand(5,1)
fileID1 = fopen('textfile1.txt','w');
Explanation: As we saw the result, the same random numbers are written in that text file. The file contents are displayed using the type function.
Let us see the example for a read operation, reading operation we take an earlier text file textfile1.txt, for a reading operation firstly we specify the type of access for that we use a fopen statement, we take fopen in parenthesis we take the text file name which we want to read (filetext1.txt) and the type of access that is reading the file which is specified by ‘r’ and these two arguments are separated by a comma. Then specify the format using ‘format spec’ we defined as afloat. Then we use a fscanf statement it is used for reading a text file. We take fsacf in parenthesis we write a fileID1 (text file is indicated by file identifier fileID1) and format specifier which we defined earlier that is formatSpec and these two arguments are separated by a comma. This read data is stored in variable A1. And we close the file using a fclose statement.
fileID1 = fopen('textfile1.txt','r');
formatSpec = '%f';A1 = fscanf(fileID1,formatSpec)fclose(fileID1);
Explanation: As we saw a result the data is displayed are similar to the earlier example result. That is shown as the read operation is successfully handled. Because read the same file which we write in an earlier example.
Let us see an example related to file handling operations, in this example, we read data from one file can that data write on another text file. For this we first read data from textfile1 using a fcanf statement then the same data can write on textfile2 file using a fprintf statement and after that, we close both the operation using a fclose. For verification, we use a type function this function is used to display the contents on file.
fileID1 = fopen('textfile1.txt','r');formatSpec = '%f';A1 = fscanf(fileID1,formatSpec)
fileID2 = fopen ('textfile2.txt','w');
In this article, we saw the different operations on file like reading and writing. We saw the different types of syntax to read or write data from the text files. And we also saw some examples for reading and writing operation that’s also called file handling.
This is a guide to Matlab File. Here we discuss an introduction to Matlab File, syntax, how does it work, examples with code, and output. You can also go through our other related articles to learn more –