Random display of 1 out of several images

2 ビュー (過去 30 日間)
Sophie Hanke
Sophie Hanke 2020 年 6 月 17 日
コメント済み: Sophie Hanke 2020 年 6 月 18 日
Dear all,
I have a folder containing 8 images and I want matlab to choose only one of those images by random to display the picture in the center of the screen. The images are in the folder /congruent and are named for example "1.bmp" or "2.bmp".
I tried the follwing code, but it doesn't work as I don't really know how to access the images in the right way.
Thank you very much in advance!!
if trialcondition == 1 % congruent condition
randomnumber = randi([1 8]);
picture = imread(fullfile(pwd,'congruent',randomnumber,'*.bmp'));
imshow(pictue);
WaitSecs(1);
elseif trialcondition == 2 % incongruent condition
% here should essentially happen the same but with a different folder
end

採用された回答

David Hill
David Hill 2020 年 6 月 17 日
編集済み: David Hill 2020 年 6 月 17 日
You could read all the images into a cell array (each image in an element), then:
for k=1:8
imageCell{k}=imread([num2str(k),'.bmp']);
end
imshow(imageCell{randi(8)});
  1 件のコメント
Sophie Hanke
Sophie Hanke 2020 年 6 月 18 日
This works! Thank you very much.

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by