File Handling in MATLAB | MATLAB Tutorial
by Ratnesh Shah
File Handling Tutorial
Data has become a paramount necessity of our day to day life. There are various ways in which we can store and access this data using different files. Thus, file handling becomes an essential need. MATLAB provides various functions to interact with the files.
loads data from an external file. It can be an image, text file or an audio file.
X = importdata(filename);
X = importdata(‘-pastespecial’) : Loads data from the system clipboard rather than from a file.
First, the image is imported into a matrix of the dimension of the image containing pixel values of R, G and B plane. As can be seen from Fig1. imshow function is used to reconstruct the image from the matrix of pixels and the original image is obtained.
Open file, or obtain information about open files
- fileID = fopen(filename) opens the file, filename, for binary read access, and returns an integer file identifier equal to or greater than 3.
- MATLAB® reserves file identifiers 0, 1, and 2 for standard input, standard output (the screen), and standard error, respectively.If fopen cannot open the file, then fileID is -1.
- fileID = fopen(filename,permission) opens the file with the type of access specified by permission.
|‘r’||Open file for reading.|
|‘w’||Open or create a new file for writing. Discard existing contents, if any.|
|‘a’||Open or create a new file for writing. Append data to the end of the file.|
|‘r+’||Open file for reading and writing.|
|‘w+’||Open or create a new file for reading and writing. Discard existing contents, if any.|
|‘a+’||Open or create a new file for reading and writing. Append data to the end of the file.|
|‘A’||Open file for appending without automatic flushing of the current output buffer.|
|‘W’||Open file for writing without automatic flushing of the current output buffer.|
Reads data from text file.
- X = fscanf(fileID,formatSpec)
- X = fscanf(fileID,formatSpec,sizeX)
- [X,cnt] = fscanf(___)
Write data to text file.
Example: This example includes the use of fopen, fscanf, fprintf & fclose.
%%Creates a file x = 1:1:5; y = [x;rand(1,5)]; fileID = fopen('test.txt','w');%If file is not created it creates it an opens it in write mode. fprintf(fileID,'%d %4.4f\n',y);%The data is written to the opened file fclose(fileID);%File is closed %%Reads the file fileID = fopen('test.txt','r');%File is opened in read mode formatSpec = '%d %f'; %Integer and float format sizeA = [2 Inf];%output matrix size A = fscanf(fileID,formatSpec,sizeA);%Reads data from the text file in the given format and creates the output in given size fclose(fileID);%Closes the file
- First, the data y is generated and then file test.txt is opened in write mode. If file named test.txt does not exist, then it is created and then opened in writing mode using the fopen
- The data in variable y is written into the file using the fprintf command in the format ‘%d %4.4f’.The contents of the file can be seen from the Fig.4.
- Next, the file is opened in read mode and then the data of the file is read in the format ‘%d %f’ using the fscanf fclose command is used to close the file once the work is over.
Reading Excel Files
We can read the data from an excel file into a matrix in MATLAB.
M = xlsread(‘filename.xlsx’);
Let say we want to read these data from the excel file into a matrix and then want to perform some operation on it.
File Containing the data:
We can see that the data from the excel file is assigned into the variable A as a matrix.