0

I'm trying to use a loop to show flashing images on the left and right sides of the screen. It's working at the moment, but showing images in the order that they appear in my folder, which is not idea as I would love them to be randomly presented. And ideas would be appreciated.

I am using the psychtoolbox in MATLAB on windows, here's my code:

%reading in all images
baseDir=pwd;
cd([baseDir,'\Images']) %change directory to images folder
jpegFiles = dir('*.jpg'); % create a cell array of all jpeg images

for k=1:size(jpegFiles,1)
images{k}=imread(jpegFiles(k).name);
end
cd(baseDir) %change directory back to the base directory


%using a loop to show images
for k=1:290
texture1(k)=Screen('MakeTexture',w,images{k});    
end
for k=1:145
Screen('DrawTexture',w,(texture1(k)), [], leftposition);
Screen('DrawTexture',w,(texture1(k+145)), [], rightposition);
Screen('DrawLines', w, allCoords,...
lineWidthPix, black, [xCenter yCenter], 2);
Screen(w,'Flip');
pause(0.2);
end
Emily
  • 29
  • 1
  • 7

1 Answers1

2

You could either use randperm to shuffle the list of images up-front.

images = images(randperm(numel(images)));

Using this approach, you'd be guaranteed that the same image would never appear twice using your methodology.

If you just want to randomly display any image (even if it's been displayed before), rather than using images{k}, you could draw the index randomly from all values between 1 and numel(images) (using randi) and display that image.

images{randi([1 numel(images)])}

or you could index into texture1 randomly.

In your code that would look something like this

nImages = numel(images);

% Loop all of this as long as you want

left_texture = texture1(randi([1 nImages]));
right_texture = texture1(randi([ 1 nImages]));

Screen('DrawTexture', w, left_texture, [], leftposition);
Screen('DrawTexture', w, right_texture, [], rightposition);
Suever
  • 64,497
  • 14
  • 82
  • 101