create four blocks of images to be shown in random order in matlab

To create four blocks of images to be shown in random order in Matlab, you can follow the steps below:

  1. Define the directory where your image files are located.
  2. Use the dir function to get a structure containing information about the image files in the directory.
  3. Extract the names of the files from the structure and store them in a cell array.
  4. Initialize a cell array to hold the image data.
  5. Use a loop to read each image file into the cell array using the imread function.
  6. Use the randperm function to generate a random permutation of the numbers from 1 to 4.
  7. Use another loop to display the images in the order specified by the random permutation using the imshow function.

Here's the code:

main.m
% Define directory where image files are located
imgDir = 'C:\Images\';

% Get structure containing information on image files in the directory
imgStruct = dir(fullfile(imgDir,'*.jpg'));

% Extract filenames from structure and store in a cell array
imgNames = {imgStruct.name};

% Initialize a cell array to hold the image data
imgData = cell(1,4);

% Loop to read each image file into the cell array
for i = 1:4
    imgData{i} = imread(fullfile(imgDir,imgNames{i}));
end

% Generate random permutation of numbers 1 to 4
perm = randperm(4);

% Loop to display the images in the order specified by the random permutation
for i = 1:4
    imshow(imgData{perm(i)});
    % code for adding any delay if required
    pause(1); % displaying each image for 1 second
end
761 chars
27 lines

In this code, the images are read into a cell array and then displayed in a random order using the imshow function. Note that you can add any delay between the display of each image using the pause function if required.

gistlibby LogSnag