MATLAB: How to present two streams of images on one screen simultaneously

MATLABpsychtoolbox

Dear members:
I am a vision researcher, currently using psychtoolbox to program my experiment, the plan is to present two streams of images with different frequencies simultaneously on one monitor.
To be more percise, I want the two scripts to start at the same time. I have tried parallel computing toolbox, but with little success, just wondering whether there is an easier way to program for what I entended.
e.g.,
Right side: imageA -> imageB -> imageA -> imageB…
nPresentations = 10
for presentationcount = 1:nPresentations %10 rounds
image = imread('imageA.jpg'); % present imageA for 1 second
texture = Screen('Maketexture', windowPtr, image);
Screen('DrawTexture', windowPtr, texture, [], destinationRect_R);
Screen('Flip', windowPtr);
WaitSecs(1);
image = imread('imageB.jpg'); % present imageB for 0.5 second
texture = Screen('Maketexture', windowPtr, image);
Screen('DrawTexture', windowPtr, texture, [], destinationRect_R);
Screen('Flip', windowPtr);
WaitSecs(0.5);
end
Left side: imageC -> imageD -> imageC -> imageD…
nPresentations = 20
for presentationcount = 1:nPresentations %20 rounds
image = imread('imageC.jpg'); % present imageC for 0.7 second
texture = Screen('Maketexture', windowPtr, image);
Screen('DrawTexture', windowPtr, texture, [], destinationRect_L); %draw texture on the left side
Screen('Flip', windowPtr);
WaitSecs(0.7);
image = imread('imageD.jpg'); % present imageD for 0.3 second
texture = Screen('Maketexture', windowPtr, image);
Screen('DrawTexture', windowPtr, texture, [], destinationRect_L);
Screen('Flip', windowPtr);
WaitSecs(0.3);
end
Thanks in advance

Best Answer

One way to do this is to use a timer callback functions: https://www.mathworks.com/help/matlab/ref/timer-class.html. For example, try the following code
im1 = imread('peacock.jpg');
im2 = imread('pears.png');
im3 = imread('cameraman.tif');
im4 = imread('carsinfront.png');
fig1 = figure();
ax1 = axes();
fig2 = figure();
ax2 = axes();
% timers for figure 1
% period of both timers is 1 + 0.5 = 1.5 seconds
% an initial delay of 0.5 is given for t1 to give some time for setup
% an initial delay of 0.5 + 1 = 1.5 is given to t2, since it need to
% lag t1 by 1 second.
t1 = timer('ExecutionMode', 'fixedRate', 'Period', 1.5, ...
'TimerFcn', {@timerFcn, ax1, im1}, 'StartDelay', 0.5);
t2 = timer('ExecutionMode', 'fixedRate', 'Period', 1.5, ...
'TimerFcn', {@timerFcn, ax1, im2}, 'StartDelay', 1.5);
t1.UserData.count = 1;
t1.UserData.max_count = 10;
t2.UserData.count = 1;
t2.UserData.max_count = 10;
% timers for figure 2
t3 = timer('ExecutionMode', 'fixedRate', 'Period', 1, ...
'TimerFcn', {@timerFcn, ax2, im3}, 'StartDelay', 0.5);
t4 = timer('ExecutionMode', 'fixedRate', 'Period', 1, ...
'TimerFcn', {@timerFcn, ax2, im4}, 'StartDelay', 1.2);
t3.UserData.count = 1;
t3.UserData.max_count = 10;
t4.UserData.count = 1;
t4.UserData.max_count = 10;
start(t1);
start(t2);
start(t3);
start(t4);
function timerFcn(obj, event, ax, im)
imshow(im, 'Parent', ax);
if obj.UserData.count == obj.UserData.max_count
stop(obj);
delete(obj);
else
obj.UserData.count = obj.UserData.count + 1;
end
end