Matlab crashes playing video's with PSB


I am trying to code my EEG experiment in Matlab using PSB on a Window 11. I played video’s and sounds before on this computer using this code. But suddenly, Matlab crashes when running the code, as such I cannot see the error causing the crash.

Can someone help me discover the problem, that would help me so much :slight_smile:? (I’ll post pieces of the code, which are of importance)

function visualonly_metreactie ()

screens = Screen('Screens');
screenNumber = max(screens);
screenSize = Screen('Rect', screenNumber);
fig = figure('MenuBar', 'none', 'Color', [0 0 0], 'Resize', 'on');
set(fig, 'WindowStyle', 'modal');
set(fig, 'WindowState', 'fullscreen');
set(fig, 'Toolbar', 'none');

white = [1 1 1];
black = [0 0 0];

% Get screen size and open black background
% Ctrl + F11 to close fullscreen
screens = Screen('Screens');
screenNumber = max(screens);
% Windowed screen
% [window, windowRect] = PsychImaging('OpenWindow', screenNumber, black, [0 0 800 600]);
% Full-width screen: 
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, black); 
ifi = Screen('GetFlipInterval', window);
Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
[~, ~] = Screen('WindowSize', window);
[~, ~] = RectCenter(windowRect);
Screen('TextSize', window, 30);
Screen('TextFont', window, 'Courier');
DrawFormattedText(window, 'Are you ready for round 1?\n\nPress the spacebar to start!', 'center', 'center', white);

% Flip to the screen
Screen('Flip', window);

while 1
     [keyIsDown, ~, keyCode] = KbCheck;
     if keyIsDown && keyCode(KbName('space'))

% Full-width screen: 
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, black); 
ifi = Screen('GetFlipInterval', window);
Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
[~, ~] = Screen('WindowSize', window);
[~, ~] = RectCenter(windowRect);
Screen('TextSize', window, 70);
presSecs = 0.5;
waitframes = round(presSecs / ifi);
currentNumber = 3;
topPriorityLevel = MaxPriority(window);
vbl = Screen('Flip', window);

while currentNumber >= 0
    numberString = num2str(currentNumber);
    DrawFormattedText(window, numberString, 'center', 'center', white);
    vbl = Screen('Flip', window, vbl + (waitframes - 1) * ifi);
    currentNumber = currentNumber - 1;

%                        Run the experiment
dirPath = 'C:\Users\Administrateur\Desktop\MATLAB\22_Pubert_Hear\pilot';
BAfileName = 'BAvideo_sound.mp4';
DAfileName = 'DAvideo_sound.mp4';
BAmoviename = fullfile(dirPath,BAfileName);
DAmoviename = fullfile(dirPath,DAfileName);
BA = VideoReader ('BAvideo_sound.mp4');
DA = VideoReader ('DAvideo_sound.mp4');

% Define StimuliList:
StimuliList = [1 1 1 2 1 1 1 1 1 1 2 1]';
ReactionList  = [0 0 1 0 0 0 0 0 1 0 0 0];
R = length(ReactionList);
StimuliMatrix = zeros(R,3);
StimuliMatrix(:,1) = StimuliList;
StimuliMatrix(:,2) = ReactionList;

% Load the reaction image
white = [1 1 1];
emoji = imread('emoji1.png');
emoji = imresize(emoji, [10, 10], 'bicubic');

[emojiWidth, emojiHeight] = size(emoji);
[screenWidth, screenHeight] = Screen('WindowSize', window);

emojiX = screenWidth/2 - emojiWidth/2;
emojiY = screenHeight/2 - emojiHeight/2;
emoji_tex = Screen('MakeTexture', window, emoji);

% Calculate the position of the texture
emojiX = screenWidth/2 - emojiWidth/2;
emojiY = screenHeight/2 - emojiHeight/2;

% Reaction-task
reaction_output = zeros(1,15);
index = 1; 
start_time = GetSecs(); %time of every stimuli played
deviceNumber = -1;
--------- until here everything works ----------

for i = 1:size(StimuliMatrix,1)
    % Get the current stimulus and reaction values
    currentStimulus = StimuliMatrix(i, 1);
    currentReaction = StimuliMatrix(i, 2);

% If the current stimulus is 1 and the current reaction is 0, play BA
        if currentStimulus == 1 && currentReaction == 0
        StimuliMatrix(i,3) = GetSecs()-start_time;
        BAmovie = Screen('OpenMovie', window, BAmoviename); 
        Screen('PlayMovie', BAmovie, 1, [], 0); 
            while hasFrame(BA)      
                tex_ba = Screen('GetMovieImage', window, BAmovie);        
                if tex_ba <= 0
                Screen('DrawTexture', window, tex_ba);              
                Screen('Close', tex_ba);                 
                Screen('Flip', window);
        Screen('PlayMovie', BAmovie, 0, [], 0);
        Screen('CloseMovie', BAmovie);

---------- the moment the video needs to play, it crashes, rest of the code is not provided, cause similar to first if -------------------

Thank you for your time,

does matlab actually crash or does your code just crash? If the latter, wrap your code in:

    % your code
catch ME

so you can see the error

A very long shot (but on windows who knows?), but what happens if you close the texture after the flip? In general you should isolate just the code that is causing the crash (using @dcnieho’s suggestion of try catch) into a new script to make it easier to debug, you can also try to enable MATLAB’s diary mode in case it is a MATLAB crash?

Thank you. Just a few hours ago it worked. Now it suddenly crashes Matlab itself again.

If I go over a simplified script only running one video ‘BAvideo.mp4’ step by step it always seems to crash when going over the following lines:
BAmovie = Screen(‘OpenMovie’, window, BAmoviename);
Screen(‘PlayMovie’, BAmovie, 1, , 0);

But I get these Errors and Info’s:

115 BAmovie = Screen(‘OpenMovie’, window, BAmoviename);
PTB-INFO: Statechange in progress with GST_STATE_CHANGE_ASYNC.
PTB-WARNING: Could not query duration of movie 0 [C:\Users\Administrateur\Desktop\MATLAB\22_Pubert_Hear\pilot\tests\BAvideo.mp4] in seconds. Returning infinity.
PTB-WARNING: Could not query position in movie 0 in seconds. Returning zero.
PTB-INFO: Statechange in progress with GST_STATE_CHANGE_ASYNC.

Your script needs serious work. Skimming it, I see lots of weird/wrong/slightly wrong stuff and am surprised it work(ed)s at all. E.g., opening a fullscreen matlab figure window (why?), opening two ptb fullscreen onscreen windows on top of that (why??), mixing PTB Screen movie playback with Matlab Videoreader, queries that query values but don’t actually store the returned data in variables. I think going carefully through tutorials and our demos and thinking about each line of code what it does and why it is (supposed to be) there, would be a good idea.

Wrt. to your error here, probably some misencoded movie file or missing codecs or something else. 1st step is always trying our own movie demos like SimpleMovieDemo(BAmoviename) etc. to see if properly written code works. Other than that, I won’t comment on this issue anymore without proof of purchase of a paid support membership. This response was already a courtesy.


I would try to not load the sound when the movie file is opened and see what happens (specialflags1 = 2 in ‘OpenMovie’, see Psychtoolbox-3 - Screen(‘OpenMovie’)). Just in case, as in macos and linux I am used to movie files containing sounds causing problems, not sure about windows.

I would try removing the videoreader calls, mario is right that this and some other parts of the code are weird/unnecessary.

I would try reencoding the video file to some other codec and see if that helps, a motion jpeg avi file is usually the safest bet ime to try.