A file name extension or simply file extension is present as a suffix in the end of a file name and comes after a period (.) and is mostly 2 to 4 characters in length. We can easily notice these extensions while opening a picture or document, by checking the name of the file. Operating systems use file extensions to identify the applications that are associated with the file types i.e. it is identified which application will be opened when we double click on the file.
Below is the table showing some of the file extensions supported by MATLAB:
|Content of the File||Extension||Details|
|MATLAB formatted data||.MAT||MATLAB workspace|
|Text||TXT, CSV etc||Comma separated valuesDelimited valuesCombination of numbers and text|
|Spreadsheet||XLSMXLSXXLS||Data stored in the excel|
|Extensible mark-up language||XML||Formatted text in XML|
|Image files||Tif||Image formats like ‘tif’, ‘jpeg’, ‘gif’ etc. are supported|
Let us now understand the syntax to open or load some of these file extensions in MATLAB:
- Load (file)
- Imread (file)
- Load(file) is used to load the variables present in the input file with ‘.MAT’ extension into our workspace
- Imread (file) will read the image which is specified by the argument ‘file’. Since we have not passed any format argument in this syntax, it will infer the format from the contents of the file
- Readmatrix is used to open a spreadsheet or a file with extensions like ‘XLSM’, ‘XLSX’, ‘XLS’
Examples of Matlab File Extension
Let us now understand the code to read a file with .MAT extension in MATLAB using ‘Load(file) function’.
In this example, we will load data from a file ‘gong.mat’ which is present in MATLAB’s directory. Below are the steps to be followed:
- Check the existing contents of the workspace
- Check the contents of the ‘gong.mat’ file
- Load the ‘gong.mat file’
<!-- wp:paragraph --> <p><code>disp('Before our file is loaded:')<br>whos</code>[Getting the existing contents of the workspace]</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p><code>disp('gong.mat file:')<br>whos('-file','gong.mat')</code>[Getting the contents of the input file to be loaded]</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p><code>load('gong.mat')</code>[Loading the file with .MAT extension]</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p><code>disp('After our file is loaded:')<br>whos</code>[Checking the contents of the workspace after loading the file]</p> <!-- /wp:paragraph -->
disp('Before our file is loaded:') whos disp('gong.mat file:') whos('-file','gong.mat') load('gong.mat') disp('After our file is loaded:') whos
As we can see in the output, the contents of the file with .MAT extension are now loaded into our workspace.
Let us now understand the code to read a file with .tif extension. As this is an image file, we will be using ‘imread’ function.
In this example, we will read the image of a moon from the ‘moon.tif’ file which is present in MATLAB’s directory. We will follow the following 2 steps:
- Call the imread function; this will read the image
- Call the imshow function; this will display the moon’s image into our workspace
A = imread(‘moon.tif’)[Calling the imread function to read the file with ‘.tif’ extension]
imshow(A)[Calling the imshow function; the image read in the first step is passed as an input to the imshow function. This will display the image into our workspace]
A = imread(‘moon.tif’)
As we can see in the output, we have obtained image of the moon, which was read by us from MATLAB’s file with ‘.tif’ extension.
Let us now understand the code to read a file with .txt extension. We will use ‘readmatrix’ function for this purpose.
In this example, we will read contents of the file ‘basic_matrix.txt’ which is present in MATLAB’s directory.
A = readmatrix('basic_matrix.txt')[Calling the readmatrix function to read the file with ‘.txt’ extension] [‘basic_matrix.txt’ file contains a 5 x 4 matrix. The above line of code will display this matrix into our workspace]
A = readmatrix('basic_matrix.txt')
As we can see in the output, we have obtained a 5 x 4 matrix which is read by us from a file with ‘.txt’ extension.
Next, we will learn how to change a file’s extension. This can be done by following the below mentioned steps:
- List all your files whose extensions you want to change in the directory
- Put the extension as ‘.out’
- Copy this file and then change its extension to the extension you want (for our understanding we will use ‘.txt’ extension)
- Delete the file with .out extension
Below is the code to change the extension of multiple files in one go:
directory = 'C:\Users\name\Documents\MATLAB'; listOfFiles = dir([directory, '*.out']);
[Give the names of all the files whose extensions are to be changed]
for i = 1 :numel (listOfFiles) file1 = fullfile(directory, listOfFiles(1).name); [temporaryDir, temporaryFile] = fileparts(file1); status = copyfile(file1, fullfile(temporaryDir, [temporaryFile, '.txt']))
[Loop to copy all .out files and give them ‘.txt’ extension]
delete(file)[Deleting the file with ‘.out’ extension]