Hello!
I was wondering if anyone could help me create a code that will allow me to randomly present 5 images that are currently in a folder on my desktop called 'happy' and for the images to stay on screen for about 1 second. I would also like for the images to be able to repeat.
I also want to be able to have people answer an either or question after each image is presented so if anyone can attach a link to where I might find examples of this part of the code, let me know!
Thank you!

 Accepted Answer

Voss
Voss on 12 Apr 2022
Edited: Voss on 12 Apr 2022
% use your folder's path, and use your file type (.png, .jpg, whatever):
% img_files = dir(fullfile('C:\Users\ja-woo\Desktop\happy','*.png'));
% I'm using just the images I attached here:
img_files = dir('image_*.png');
N = numel(img_files); % number of image files
figure(); % create a figure where the images will be shown
idx = []; % this will store the index of each file used (1 through N)
answers = {}; % this will store the answers (Happy/Sad - you can change the options below)
while true
new_idx = randi(N); % random integer from 1 to N
imshow(imread(fullfile(img_files(new_idx).folder,img_files(new_idx).name))); % read and show the idx-th file
pause(1); % pause for 1 second
new_answer = questdlg('How does this image make you feel?','','Happy','Sad','Happy'); % ask the question and get the answer
if isempty(new_answer) % if the user didn't answer, exit the loop (i.e., stop showing images and stop asking questions)
break
end
idx(end+1) = new_idx; % add the latest image index to the array of indices
answers{end+1} = new_answer; % add the latest answer to the array of answers
end
disp(idx); % show the indexes that were used (e.g., 1, 3, 1, 2, 5, 1, 4)
disp({img_files(idx).name}); % show the names of the files that were used (e.g., image_1.png, image_3.png, image_1.png, image_2.png, image_5.png, image_1.png, image_4.png)
disp(answers); % show the answers given (e.g., Happy, Sad, Happy, Happy, Sad, Sad, Happy)

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 12 Apr 2022

Commented:

DGM
on 12 Apr 2022

Community Treasure Hunt

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

Start Hunting!