How to display an image randomly

I am trying to display stimuli, but struggling as I am new to Matlab. How can I display stimuli (images) randomly from a folder in my documents? I want to use every image, but don't want to use the same picture more than once. Thanks
To be more clear, if I have an image in Documents --> Thesis --> Images how do I call this image to be displayed?

Answers (1)

Guillaume
Guillaume on 9 Oct 2017
Edited: Guillaume on 9 Oct 2017
Use randperm to create a random ordering of your images. The code would go something like:
root = 'C:\somewhere\somefolder';
images = dir(fullfile(root, '*.png')); %or whatever extension you use
order = randperm(numel(images));
for idx = 1:numel(img)
currentimg = imread(fullfile(root, images(order(idx)).name));
imshow(currentimg);
%...
end

3 Comments

the screen just turned black and didn't do anything... any idea what I could be doing wrong?
Impossible to know because:
  • "the screen just turned black and didn't do anything" is meaningless as a diagnostic. Surely, your whole computer monitor didn't turn black. And what did you expect the screen to do?
  • we don't know what exact code you used. Note that the %... was meant for you to add whatever processing you wanted to do. If you were to use the code above without any modification then at the very least I would add a drawnow and a pause(1).
If the images do not display properly, you can replace the imshow call by
imshow(currentimg, []);
read the documentation of imshow to know the difference between the two syntax.
Also, add
drawnow() after the imshow()

Sign in to comment.

Asked:

on 9 Oct 2017

Commented:

on 16 Oct 2017

Community Treasure Hunt

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

Start Hunting!