MATLAB: How to store images in a single array or matrix

image processingImage Processing Toolboxmontage

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).

Best Answer

  • 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.