How to present many images repeatedly and randomly? Problem Function

1 view (last 30 days)
Hi all,
Matlab is still a new language to me and I would like to present 360 experimental trials based on 40 images (I want those images to be repeated and it should be presented randomly). I created the following Function but none of the parts works correctly :( Anybody to help?
% X
%This function is created to test the EGI system. Faces will be presented, half
%will be inverted.
function X
Screen('Preference', 'SkipSyncTests', 1)
%Open the screen
[wPtr,rect]=Screen('OpenWindow',max(Screen('Screens')),[0 0 600 600]);
xCenter=rect(3)/2;
yCenter=rect(4)/2;
imgPath = 'C:/Users/X/Desktop/MatlabTutorial/EGItest';
imgType = '*.jpeg';
images = dir(fullfile(['C:/Users/X/Desktop/MatlabTutorial/EGItest', '*.jpeg']));
%Create textures
for n=1:20
images{n}=imread(sprintf('C:/Users/X/Desktop/MatlabTutorial/EGItest', '*.jpeg'));
end
imageData=imread('C:/Users/X/Desktop/MatlabTutorial/EGItest', '*.jpeg');
imageTexture=Screen('MakeTexture', wPtr,imageData);
%Get size of image (all images the same size in this example)
%imageHeight
%imageWidth
%colorChannels
[imageHeight, imageWidth,colorChannels]=size(imageData);
%Set up left and right picture locations
gap=100; %distance of pics from center
leftRect=[xCenter-gap-imageWidth, yCenter-imageHeight/2, xCenter+imageHeight/2];
rightRect=[xCenter+gap, yCenter-imageHeight/2, xCenter+gap+imageWidth, yCenter+imageHeight/2];
%set up some vectors with our options
allFiles=dir('C:/Users/X/Desktop/MatlabTutorial/EGItest', '*.jpeg');
allNames={allFiles.jpeg};
textures=[imageTexture];
textureNames={'allFiles'};
rects={leftRect, rightRect};
rectNames={'left', 'right'};
%loop for 200 trials
for trial=1:200
file_name=dir(strcat('C:/Users/X/Desktop/MatlabTutorial/EGItest', '*.jpeg'));
im=imread(strcat('C:/Users/X/Desktop/MatlabTutorial/EGItest','*.jpeg'));
imtool(im);
repelem(v,3);
end
%pick a random number 1 or 2
randTextureNum=randi(2);
%now use that number tp pick a texture
ourTexture=textures(randTextureNum);
%draw the pie
%windowPtr=wPtr
Screen('DrawTexture', wPtr, ourTexture,[], ourRect);
stimTime=Screen('Flip', wPtr);
end

Accepted Answer

Walter Roberson
Walter Roberson on 7 Aug 2015
images_info = dir(fullfile(imgPath, imgType));
numImage = length(images_info);
images = cell(numImage,1);
for K = 1 : numImage
images{K} = imread( fullfile(imgPath, images_info(K).name) );
end
To go further we need to know how you want the 9 copies of each of the 40 images to be presented relative to each other. Do you want all 40 images once each in a random order, and then the same order repeated another 8 times? Do you want all 40 images once each in a random order, and then the 40 images once each in a different random order, and so on, each time changing the order of the group of 40? Do you want the order to be completely randomized as long as each of the 40 is shown 9 times exactly? So the first image might again be shown as the 17th image and again the 23rd image and then might not show up for another 100 images? Do you want the oder to be completely randomized but the same image cannot show up within some number N of the previous time? Are you doing 9 different things for each image that need to be tracked? Is the first thing done on the first cycle of 40, the second thing done on the second cycle of 40, and so on? Or should the order of the different things for each image be random?
imagenum = 1:40;
thingnum = 1:9;
[IMAGENUM, THINGNUM] = ndgrid(imagenum, thingnum);
pair_specs = [IMAGENUM(:), THINGNUM(:)];
randorder = randperm(size(pair_specs,1));
rand_specs = pair_specs(randorder,:);
  4 Comments
LuS
LuS on 7 Aug 2015
As you may see I am still working on my function with no success. Is it possible to check what blocks me from running the script?
Thanks a lot!
function X
Screen('Preference', 'SkipSyncTests', 1);
%Open the screen
[wPtr,rect]=Screen('OpenWindow',max(Screen('Screens')), [], [0 0 1400 1400]);
xCenter=rect(3)/2;
yCenter=rect(4)/2;
%set up some vectors with our options
allFiles=dir('C:/Users/X/Desktop/MatlabTutorial/EGItest/*.jpeg');
allNames={allFiles};
images_info=dir(fullfile('C:/Users/X/Desktop/MatlabTutorial/EGItest/*.jpeg'));
numImage=length(images_info);
images=cell(numImage,1);
for K=1:numImage
images{k}=imread(fullfile('C:/Users/X/Desktop/MatlabTutorial/EGItest/*.jpeg', images_info(k).name));
end
imagenum=1:20;
thingnum=1:9;
[IMAGENUM, THINGNUM]=ndgrid(imagenum,thingnum);
pair_specs=[IMAGENUM(:), THINGNUM(:)];
randorder=randperm(size(pair_specs,1));
rand_specs=pair_specs(randorder,:);
T=repmat(1:numImage, 1, 9);
Trand=T(randperm(length(T)));
for K=1:length(Trand)
ourTexture=Screen('MakeTexture', wPtr,images);
Screen('DrawTexture', wPtr, ourTexture,[], ourRect);
stimTime=Screen('Flip', wPtr);
imtool(images{k});
repelem(v,3);
end
%Get size of image (all images the same size in this example)
%imageHeight
%imageWidth
%colorChannels
[imageHeight, imageWidth,colorChannels]=size(0);
%Set up left and right picture locations
gap=100; %distance of pics from center
leftRect=[xCenter-gap-imageWidth, yCenter-imageHeight/2, xCenter+imageHeight/2];
rightRect=[xCenter+gap, yCenter-imageHeight/2, xCenter+gap+imageWidth, yCenter+imageHeight/2];
%loop for 180 trials
for trial=1:180
file_name=dir(strcat('C:/Users/X/Desktop/MatlabTutorial/EGItest/*.jpeg'));
end
%pick a random number 1 or 2
randTextureNum=randi(2);
%pick a random number 1 or 2
randRectNum=randi(2);
end
Walter Roberson
Walter Roberson on 7 Aug 2015
Please go back and incorporate the style I showed at the beginning of my Answer, the code before "To go further". That is, do not keep reading the files, do not hardcode the directories, use fullfile().
Also you are going to need to say what problem you are encountering. You have not admitted yet that you are using the psychtools toolbox; I do not have that package installed.

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!