.mat converstion to jpg png jpeg etc
5 views (last 30 days)
Show older comments
How i can convert the whole data set into jpg jpeg formate from .mat formate? currently each image is in .mat formate
1 Comment
Walter Roberson
on 11 Apr 2021
Is it one mat file for each image? Is the variable name in the mat file consistent?
Are all images in the same .mat file? Are they in separate variables?
Answers (1)
Gayathri
on 10 Jun 2025
Inorder to convert the images in '.mat' format to '.jpg', we should load each file and then convert using a "for loop". Please refer to the code below for achieving the same.
% Specify the folder containing the .mat files
folderPath = 'path_to_your_mat_files'; % Replace with your folder path
matFiles = dir(fullfile(folderPath, '*.mat')); % Get all .mat files
% Loop through each .mat file
for k = 1:length(matFiles)
% Load the .mat file
matFileName = fullfile(folderPath, matFiles(k).name);
data = load(matFileName);
% Extract the image data (adjust the variable name as necessary)
img = data.imageData; % Replace 'imageData' with the actual variable name
% Convert to uint8 if necessary
img = im2uint8(img); % Ensure the image is in the correct format
% Create a filename for the output image
[~, name, ~] = fileparts(matFiles(k).name); % Get the base name of the file
outputFileName = fullfile(folderPath, [name, '.jpg']); % Change extension to .jpg
% Save the image
imwrite(img, outputFileName);
end
The above code is written assuming that each image is stored in a separate '.mat' file. If it is a single MAT file for all images please modify the code accordingly.
For more information on "imwrite" function, refer to the following documentation link.
0 Comments
See Also
Categories
Find more on Import, Export, and Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!