Psychtoolbox freezing unexpectedly (contrast gratings)

Hello! I have encountered a problem while using some features of the toolbox.

The issue occurs in a script that I have that runs contrast grating on the screen. The script also runs through a MATLAB application (App Designer). The program is made to run a hundred trials. For each trial, there are two presentations of the patches for one second each with a short break in between. Despite this working for one trial, however, it often fails to run for several trials. In my experience, the screen has frozen after seven and after sixty trials. I would like to know if you would have any suggestions for improving the performance of the program or contrast grating functions.

Moreover, when the script starts a new trial, it takes anywhere from two to fifteen seconds for the patches to appear, which may be the reason why the script freezes after a certain amount of trials.

Thank you in advance for your help!