title: MatLab: How do I randomly present pictures?
body: <p>I need to learn how to randomly present a series of pictures in MatLab. This is essential to a lot of experiments in cognitive psychology. So far I can just present the pics. Can someone tell me how to modify my existing code to present the pictures randomly? Many thanks from a beginner!</p>
  
  <p>Here's my current code: </p>
  
  <pre><code>close all
  clear all
  sca
  clc
  
  %Finds screen and uses it
  screens = Screen('Screens');
  screenNumber = max(screens);
  
  %Gather all pictures to be used. Tell comp where they are. Make them all variables
  StimuliFolder='C:\Matlab Stuff\'; %tells us where to look for image
  N=imread([StimuliFolder 'nadal.jpg']); %reads the image from the following location
  K=imread([StimuliFolder 'nishikori.jpg']); %reads the image from the following location
  F=imread([StimuliFolder 'fed.jpg']); %reads the image from the following location
  J=imread([StimuliFolder 'Bistable_Jazz_Woman.jpg']);
  H=imread([StimuliFolder 'Bistable_Horse_face.jpg']);
  D=imread([StimuliFolder 'daliface.jpg']);
  
  
  %Define and use color for screen background
  white = WhiteIndex(screenNumber);
  
  %Opens window
  [window, windowRect] = PsychImaging('OpenWindow', screenNumber, white);
  
  %Create a welcome screen that has text
  Screen('TextSize', window, 40);
  Screen('Textfont', window, 'Papyrus');
  Screen('TextStyle', window, 1);
  Screen('DrawText', window, 'Down the rabbit hole we go..', 300, 250, [0, 0, 0]);
  Screen('TextSize', window, 20);
  Screen('DrawText', window, '&lt; When ready press any key to continue &gt;', 300, 550, [0, 130, 150]);
  Screen('Flip', window);
  KbStrokeWait; %Press any key to continue
  
  % Make the Nadal image into a texture and present it
  imageTexture = Screen('MakeTexture', window, N);
  Screen('DrawTexture', window, imageTexture, [], [], 0);
  Screen('Flip', window);
  WaitSecs(1);
  
  %Make the Fed image into a texture and present it
  imageTexture = Screen('MakeTexture', window, F);
  Screen('DrawTexture', window, imageTexture, [], [], 0);
  Screen('Flip', window);
  WaitSecs(1);
  
  %Make the Nishikori image into a texture and present it
  imageTexture = Screen('MakeTexture', window, K); %refer to list, curly bracket and number in list
  Screen('DrawTexture', window, imageTexture, [],[], 0);
  Screen('Flip', window);
  WaitSecs(1)
  
  %Make and present Bistable Jazz face
  imageTexture = Screen('MakeTexture', window, J);
  Screen('DrawTexture', window, imageTexture, [], [], 0);
  Screen('Flip', window);
  WaitSecs(1)
  
  %Make and present Bistable horse face
  imageTexture = Screen('MakeTexture', window, H);
  Screen('DrawTexture', window, imageTexture, [], [], 0);
  Screen('Flip', window);
  WaitSecs(1)
  
  %Make and present Dali painting
  imageTexture = Screen('MakeTexture', window, D);
  Screen('DrawTexture', window, imageTexture, [], [], 0);
  Screen('Flip', window);
  WaitSecs(1)
  
  %Goodbye screen
  Screen('TextSize', window, 40);
  Screen('Textfont', window, 'Papyrus');
  Screen('TextStyle', window, 1);
  Screen('DrawText', window, '...and out the other end', 300, 250, [0, 0, 0]);
  Screen('DrawText', window, '&lt; Press any key to exit &gt;', 300, 550, [0, 130, 150]);
  Screen('Flip', window);
  KbStrokeWait; %Press any key to escape
  
  sca %Closes the final screen when done
  </code></pre>