.mat converstion to jpg png jpeg etc

5 visualizaciones (últimos 30 días)
Asaf Raza
Asaf Raza el 11 de Abr. de 2021
Respondida: Gayathri el 10 de Jun. de 2025
How i can convert the whole data set into jpg jpeg formate from .mat formate? currently each image is in .mat formate
  1 comentario
Walter Roberson
Walter Roberson el 11 de Abr. de 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?

Iniciar sesión para comentar.

Respuestas (1)

Gayathri
Gayathri el 10 de Jun. de 2025
Hello @Asaf Raza,
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.

Categorías

Más información sobre Import, Export, and Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by