0

ループを使用して、画面の左右に点滅する画像を表示しようとしています。現時点では機能していますが、フォルダーに表示される順序で画像を表示しています。ランダムに表示したいので、これは考えられません。そして、アイデアをいただければ幸いです。

私は Windows 上の MATLAB で psychtoolbox を使用しています。これが私のコードです。

%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
4

1 に答える 1

2

randperm前もって画像のリストをシャッフルするために使用することもできます。

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

このアプローチを使用すると、方法論を使用して同じ画像が 2 回表示されないことが保証されます。

を使用するのではなく、(以前に表示されていた場合でも)任意の画像をランダムに表示したい場合は、と のimages{k}間のすべての値からランダムにインデックスを描画し1(numel(images)を使用randi)、その画像を表示できます。

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

texture1または、ランダムにインデックスを作成できます。

あなたのコードでは、次のようになります

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);
于 2016-06-16T14:55:15.893 に答える