Updated March 13, 2023
Introduction to Matlab File Extension
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 |
|
MATLAB workspace |
Text |
|
|
Spreadsheet |
|
|
Extensible mark-up language |
|
|
Image files |
|
|
Let us now understand the syntax to open or load some of these file extensions in MATLAB:
Syntax:
- Load (file)
- Imread (file)
- readmatrix
Description:
- 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’.
Example #1
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’
Code:
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
Input:
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
Output:
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.
Example #2
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
Code:
A = imread(‘moon.tif’)
imshow(A)
Input:
A = imread(‘moon.tif’)
imshow(A)
Output:
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.
Example #3
In this example, we will read contents of the file ‘basic_matrix.txt’ which is present in MATLAB’s directory.
Code:
A = readmatrix('basic_matrix.txt')
Input:
A = readmatrix('basic_matrix.txt')
Output:
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']);
for i = 1 :numel (listOfFiles)
file1 = fullfile(directory, listOfFiles(1).name);
[temporaryDir, temporaryFile] = fileparts(file1);
status = copyfile(file1, fullfile(temporaryDir, [temporaryFile, '.txt']))
delete(file)
end
Recommended Articles
This is a guide to Matlab File Extension. Here we also discuss the introduction and syntax of matlab file extension along with different examples and its code implementation. You may also have a look at the following articles to learn more –