MATLAB Answers

i need to convert a folder of 200 .jpg images to .mat (to create 200 separate .mat files). i know there should be an easy matlab for loop to do this but I need help (newbie). thanks!

87 views (last 30 days)
Esther Mead
Esther Mead on 21 Apr 2019
Answered: Guillaume on 23 Apr 2019
i need to convert a folder of 200 .jpg images to .mat (to create 200 separate .mat files). i know there should be an easy matlab for loop to do this but I need help (newbie). thanks!

  2 Comments

Esther Mead
Esther Mead on 21 Apr 2019
sorry, put my response in wrong place..
my reply:
thanks for your reply. oh man i've tried to look around for an answer for about four or more hours. so far, i've tried opening the matlab image batch processor, importing all of my jpg files and tried to apply a function to them all.
im = imread(‘file_name.jpg’);
save(‘file_name.mat’, ‘im’);
where i replace the above with the actual name of the individual filename, e.g., '0000.jpg' and '0000.mat'...
but, it only worked for the first file. because i don't know how to write the function to find the name of the file and set it to the same name but just convert from jpg to mat. i hope i'm making sense. again i'm new. thanks again!

Sign in to comment.

Answers (3)

Guillaume
Guillaume on 23 Apr 2019
folder = 'C:\somewhere\somefolder';
filelist = dir(fullfile(folder, '*.jpg')); %get list of all jpg files in the folder
matnames = regexprep({filelist.name}, '\.jpg$', '.mat'); %replace .jpg by .mat in all file names
for fileidx = 1:numel(filelist)
img = imread(fullfile(folder, filelist(fileidx).name)); %read image
save(matnames{fileidx}, img); %save in respective mat file
end
As for saving in HDF5 format, it's unclear if you want one file per image or just one hdf5 file with a dataset per image. In any case, you wouldn't use save as you have written but h5create and h5write.

  0 Comments

Sign in to comment.


Rik
Rik on 21 Apr 2019
You're quite close. You were only missing the way to generate char arrays from a pattern. To do that you can use the sprintf function. You will find a small example below. If you don't understand the syntax I encourage you to read the documentation. Matlab has excelent documentation with many examples.
for n=1:200
im = imread(sprintf('%04d.jpg',n));
save(sprintf('%04d.mat',n),'im');
end

  0 Comments

Sign in to comment.


Image Analyst
Image Analyst on 21 Apr 2019
See the FAQ for code samples.

  8 Comments

Show 5 older comments
Esther Mead
Esther Mead on 23 Apr 2019
what i want to do is this, but for 210 images:
im = imread('00000000.jpg');
save('00000000.mat','im');
and then
im = imread('00000000.jpg');
save('00000000.h5','im');
so, for each of my 210 images, i want it in .jpg, .mat., and .h5
Rik
Rik on 23 Apr 2019
Apparently the file 00000001.jpg doesn't exist, so the underlying assumption of my code isn't true. You don't need the batch processor.
Did you make sure the current folder contains the files? Otherwise you will need to put the full path in your loader.
Also, the save function does not support saving a h5 file.

Sign in to comment.

Sign in to answer this question.