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?

回答 (1 件)

Guillaume
Guillaume 2017 年 10 月 9 日
編集済み: Guillaume 2017 年 10 月 9 日

1 投票

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 件のコメント

Erin Krahn
Erin Krahn 2017 年 10 月 16 日
the screen just turned black and didn't do anything... any idea what I could be doing wrong?
Guillaume
Guillaume 2017 年 10 月 16 日
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.
Walter Roberson
Walter Roberson 2017 年 10 月 16 日
Also, add
drawnow() after the imshow()

サインインしてコメントする。

質問済み:

2017 年 10 月 9 日

コメント済み:

2017 年 10 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by