How to store images in a single array or matrix?

Illustration
Bashir - 2022-11-22T10:27:13+00:00
Question: How to store images in a single array or matrix?

Hello, i have a number of jpeg images stored in a folder; how can i read all the images and store them as a single matrix or array (reserving the image format).  

Expert Answer

Profile picture of Prashant Kumar Prashant Kumar answered . 2025-11-20

Straight out of the FAQ:

 

myFolder = 'C:\Documents and Settings\yourUserName\My Documents\My Pictures';
if ~isdir(myFolder)
  errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
  uiwait(warndlg(errorMessage));
  return;
end
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
  baseFileName = jpegFiles(k).name;
  fullFileName = fullfile(myFolder, baseFileName);
  fprintf(1, 'Now reading %s\n', fullFileName);
  imageArray = imread(fullFileName);
  imshow(imageArray);  % Display image.
  drawnow; % Force display to update immediately.
end

Obviously, you can adapt as needed, because I'm not sure what you mean when you say "store them as a single matrix". There is a montage() function that will do that if that's what you want. It will stitch together all the image files into one giant image.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!