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)));

使用这种方法,您可以保证使用您的方法不会出现两次相同的图像。

如果您只想随机显示任何图像(即使它之前已显示),而不是使用,您可以从和(使用)images{k}之间的所有值中随机绘制索引并显示图像。1numel(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 回答