只执行了部分代码,不显示任何错误信息
Only a portion of the code is executed and no error message is displayed
我在 Mac OS 上使用 MATLAB 2020 Psychtoolbox,每次我 运行 这段代码时,它们的屏幕都会变灰,然后是错误声音,但是,没有显示任何错误消息在命令 window 中。如何执行所有代码?
% Clear the workspace
close all;
clearvars;
sca;
% Setup PTB with some default values
PsychDefaultSetup(2);
% Seed the random number generator. Here we use the an older way to be
% compatible with older systems. Newer syntax would be rng('shuffle'). Look
% at the help function of rand "help rand" for more information
rand('seed', sum(100 * clock));
% Set the screen number to the external secondary monitor if there is one
% connected
screenNumber = max(Screen('Screens'));
% Define white, grey and black
white = WhiteIndex(screenNumber);
grey = white / 2;
black = BlackIndex(screenNumber);
% Open an on screen window
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey);
% Get the size of the on screen window
[screenXpixels, screenYpixels] = Screen('WindowSize', window);
% Query the frame duration
ifi = Screen('GetFlipInterval', window);
% Set up alpha-blending for smooth (anti-aliased) lines
Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
% Setup the text type for the window
Screen('TextFont', window, 'Ariel');
Screen('TextSize', window, 36);
% Get the centre coordinate of the window
[xCenter, yCenter] = RectCenter(windowRect);
%----------------------------------------------------------------------
% Keyboard information
%----------------------------------------------------------------------
% Define the keyboard keys that are listened for. We will be using the left
% and right arrow keys as response keys for the task and the escape key as
% a exit/reset key
escapeKey = KbName('ESCAPE');
leftKey = KbName('LeftArrow');
rightKey = KbName('RightArrow');
downKey = KbName('DownArrow');
%----------------------------------------------------------------------
% Fixation cross
%----------------------------------------------------------------------
% Here we set the size of the arms of our fixation cross
fixCrossDimPix = 10;
% Now we set the coordinates (these are all relative to zero we will let
% the drawing routine center the cross in the center of our monitor for us)
xCoords = [-fixCrossDimPix fixCrossDimPix 0 0];
yCoords = [0 0 -fixCrossDimPix fixCrossDimPix];
allCoords = [xCoords; yCoords];
% Set the line width for our fixation cross
lineWidthPix = 4;
%----------------------------------------------------------------------
% Colors
%----------------------------------------------------------------------
% We are going to use three colors for this demo. Red, Green and blue.
wordList = {'Green', 'Magenta', 'Orange'};
Colors = [0 1 0; 1 0 1; 0.8500 0.3250 0.0980];
%----------------------------------------------------------------------
% Define positions of sequences
%----------------------------------------------------------------------
leftX = screenXpixels/2.5;
leftY = screenXpixels/1.6;
rightX = screenXpixels/1.7;
rightY = screenYpixels/1.6;
upX = screenXpixels/2.1;
upY = screenYpixels/2.6;
left = [leftX leftY];
right = [rightX rightY];
up = [upX upY];
%----------------------------------------------------------------------
% Randomise temporal order of trials
%----------------------------------------------------------------------
trialorder = [1 0 0 0 0];
randtemp = shuffle(trialorder);
%----------------------------------------------------------------------
% Trial loop
%----------------------------------------------------------------------
nTrials = 5;
for trial = 1:randtemp
% randomise position of sequences
randpos = shuffle(left, right, up);
% fixation cross
% Draw the fixation cross in white, set it to the center of our screen and
% set good quality antialiasing
Screen('DrawLines', window, allCoords,...
lineWidthPix, white, [xCenter yCenter], 2);
% Derive distributions
seq1 = distribution(0.02, 0.126, 0.146, 0.106, 5); % high variance
seq2 = distribution(0.02, 0.126, 0.146, 0.106, 5); % low variance
seq3 = distribution(0.02, 0.126, 0.146, 0.106, 5); % low variance
seq4 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq5 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq6 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq7 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq8 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq9 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq10 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq11 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq12 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq13 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq14 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq15 = distribution(0.02, 0.126, 0.146, 0.106, 5);
if trial == 1
DrawFormattedText(window, 'Name the color \n\n Press Any Key To Begin',...
'center', 'center', black);
Screen('Flip', window);
KbStrokeWait;
end
if randtemp() == 0
% trail 1
Screen('DrawText', num2str(seq1), white, randpos(1));
Screen('DrawText', num2str(seq2), white, randpos(2));
Screen('DrawText', num2str(seq3), white, randpos(3));
WaitSecs(0.75)
Screen('Flip', window);
KbStrokeWait;
Screen('DrawText', num2str(seq2), white, randpos(1));
Screen('DrawText', num2str(seq3), white, randpos(2));
WaitSecs(0.75)
Screen('Flip', window);
KbStrokeWait;
%trial 2
%repeat
else
Screen('DrawText', num2str(seq13), white, randpos(1));
Screen('DrawText', num2str(seq14), white, randpos(2));
Screen('DrawText', num2str(seq15), white, randpos(3));
WaitSecs(0.75)
Screen('Flip', window);
KbStrokeWait;
Screen('DrawText', num2str(seq13), white, randpos(1));
Screen('DrawText', num2str(seq14), white, randpos(2));
WaitSecs(0.75)
Screen('Flip', window);
KbStrokeWait;
end
end
% Flip to the screen
Screen('Flip', window);
% Wait for a key press
KbStrokeWait;
% Clear the screen
sca
'distribution'函数是一个自制函数,生成具有指定均值、方差、上限、下限和给定数量的随机数的高斯分布:
function distribution(va, mu, ul, ll, nvals)
multiplier=10;
x = mu + randi(multiplier*nvals,1)*sqrt(va); % Generate sufficient random numbers
idx = (ll <= x) & (x <= ul); % Extract the value in the given range [min max]
while sum(idx)<nvals
multiplier=multiplier+1;
x = mu + randi(multiplier*nvals,1)*sqrt(va); % Generate sufficient random numbers
idx = (ll <= x) & (x <= ul); % Extract the value in the given range [min max]
end
x = x(idx);
x = x(1:nvals); % Extract numbers
您只看到灰色屏幕,因为该错误不会导致 Psychtoolbox window(您已将其设置为灰色背景)关闭。要关闭屏幕并显示错误,请将代码包装在 try、catch 语句中,如果遇到错误,它将关闭屏幕并重新抛出错误。
例如(这里的CODE这个词只是一个占位符,而不是用你的实际代码替换它)
try
CODE
catch e
sca;
rethrow(e)
end
在这种情况下,如果在 try 语句的主体内遇到错误,它将转到 catch 语句,调用错误 e
。首先屏幕将用 sca
关闭,然后通过 rethrow
.
实际显示错误
就您遇到的实际错误而言,我认为您当前的代码至少存在一些问题:
Psychtoolbox 中的 Shuffle 函数大写 - 'Shuffle' 和
有一个输入。在这里,您使用 'shuffle',并提供
三个独立的输入。
不是提供 'left'、'right' 和 'up' 作为单独的输入,
将它们组合成单个元胞数组的元素:
正在替换:randpos = shuffle(left, right, up);
与:randpos = Shuffle({left, right, up});
和 select 它们作为元胞数组的元素:
正在替换:Screen('DrawText', num2str(seq1), white, randpos(1));
与:Screen('DrawText', window, num2str(seq1), white, [], black, randpos{1}(1), randpos{1}(2));
在您定义的分布函数中,您没有包含输出参数。将您当前的第一个函数行替换为:function x = distribution(va, mu, ul, ll, nvals)
没有输入参数的 KbStrokeWait
只检查连接的第一个键盘。例如,如果您在笔记本电脑上使用外部键盘,这可能会忽略按键。使用 KbStrokeWait(-3)
将检查所有连接的设备。
你的分布函数似乎要么没有收敛于一个解决方案,要么花了很长时间来确定一个解决方案,因为它似乎是随机生成值然后检查它是否足够所需的间隔。相反,我会从生成随机正常值的内置函数开始。例如,以下将从均值为 0.02 且方差为 0.126 的分布生成 5 个值。它没有实现检查所有生成的值是否在给定范围内,但是:randn(1, 5) * sqrt(0.126) + 0.02;
您在调用 DrawText 时缺少 window 指针。
可能还有其他问题,但解决这些问题会让您入门。
我在 Mac OS 上使用 MATLAB 2020 Psychtoolbox,每次我 运行 这段代码时,它们的屏幕都会变灰,然后是错误声音,但是,没有显示任何错误消息在命令 window 中。如何执行所有代码?
% Clear the workspace
close all;
clearvars;
sca;
% Setup PTB with some default values
PsychDefaultSetup(2);
% Seed the random number generator. Here we use the an older way to be
% compatible with older systems. Newer syntax would be rng('shuffle'). Look
% at the help function of rand "help rand" for more information
rand('seed', sum(100 * clock));
% Set the screen number to the external secondary monitor if there is one
% connected
screenNumber = max(Screen('Screens'));
% Define white, grey and black
white = WhiteIndex(screenNumber);
grey = white / 2;
black = BlackIndex(screenNumber);
% Open an on screen window
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey);
% Get the size of the on screen window
[screenXpixels, screenYpixels] = Screen('WindowSize', window);
% Query the frame duration
ifi = Screen('GetFlipInterval', window);
% Set up alpha-blending for smooth (anti-aliased) lines
Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
% Setup the text type for the window
Screen('TextFont', window, 'Ariel');
Screen('TextSize', window, 36);
% Get the centre coordinate of the window
[xCenter, yCenter] = RectCenter(windowRect);
%----------------------------------------------------------------------
% Keyboard information
%----------------------------------------------------------------------
% Define the keyboard keys that are listened for. We will be using the left
% and right arrow keys as response keys for the task and the escape key as
% a exit/reset key
escapeKey = KbName('ESCAPE');
leftKey = KbName('LeftArrow');
rightKey = KbName('RightArrow');
downKey = KbName('DownArrow');
%----------------------------------------------------------------------
% Fixation cross
%----------------------------------------------------------------------
% Here we set the size of the arms of our fixation cross
fixCrossDimPix = 10;
% Now we set the coordinates (these are all relative to zero we will let
% the drawing routine center the cross in the center of our monitor for us)
xCoords = [-fixCrossDimPix fixCrossDimPix 0 0];
yCoords = [0 0 -fixCrossDimPix fixCrossDimPix];
allCoords = [xCoords; yCoords];
% Set the line width for our fixation cross
lineWidthPix = 4;
%----------------------------------------------------------------------
% Colors
%----------------------------------------------------------------------
% We are going to use three colors for this demo. Red, Green and blue.
wordList = {'Green', 'Magenta', 'Orange'};
Colors = [0 1 0; 1 0 1; 0.8500 0.3250 0.0980];
%----------------------------------------------------------------------
% Define positions of sequences
%----------------------------------------------------------------------
leftX = screenXpixels/2.5;
leftY = screenXpixels/1.6;
rightX = screenXpixels/1.7;
rightY = screenYpixels/1.6;
upX = screenXpixels/2.1;
upY = screenYpixels/2.6;
left = [leftX leftY];
right = [rightX rightY];
up = [upX upY];
%----------------------------------------------------------------------
% Randomise temporal order of trials
%----------------------------------------------------------------------
trialorder = [1 0 0 0 0];
randtemp = shuffle(trialorder);
%----------------------------------------------------------------------
% Trial loop
%----------------------------------------------------------------------
nTrials = 5;
for trial = 1:randtemp
% randomise position of sequences
randpos = shuffle(left, right, up);
% fixation cross
% Draw the fixation cross in white, set it to the center of our screen and
% set good quality antialiasing
Screen('DrawLines', window, allCoords,...
lineWidthPix, white, [xCenter yCenter], 2);
% Derive distributions
seq1 = distribution(0.02, 0.126, 0.146, 0.106, 5); % high variance
seq2 = distribution(0.02, 0.126, 0.146, 0.106, 5); % low variance
seq3 = distribution(0.02, 0.126, 0.146, 0.106, 5); % low variance
seq4 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq5 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq6 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq7 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq8 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq9 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq10 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq11 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq12 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq13 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq14 = distribution(0.02, 0.126, 0.146, 0.106, 5);
seq15 = distribution(0.02, 0.126, 0.146, 0.106, 5);
if trial == 1
DrawFormattedText(window, 'Name the color \n\n Press Any Key To Begin',...
'center', 'center', black);
Screen('Flip', window);
KbStrokeWait;
end
if randtemp() == 0
% trail 1
Screen('DrawText', num2str(seq1), white, randpos(1));
Screen('DrawText', num2str(seq2), white, randpos(2));
Screen('DrawText', num2str(seq3), white, randpos(3));
WaitSecs(0.75)
Screen('Flip', window);
KbStrokeWait;
Screen('DrawText', num2str(seq2), white, randpos(1));
Screen('DrawText', num2str(seq3), white, randpos(2));
WaitSecs(0.75)
Screen('Flip', window);
KbStrokeWait;
%trial 2
%repeat
else
Screen('DrawText', num2str(seq13), white, randpos(1));
Screen('DrawText', num2str(seq14), white, randpos(2));
Screen('DrawText', num2str(seq15), white, randpos(3));
WaitSecs(0.75)
Screen('Flip', window);
KbStrokeWait;
Screen('DrawText', num2str(seq13), white, randpos(1));
Screen('DrawText', num2str(seq14), white, randpos(2));
WaitSecs(0.75)
Screen('Flip', window);
KbStrokeWait;
end
end
% Flip to the screen
Screen('Flip', window);
% Wait for a key press
KbStrokeWait;
% Clear the screen
sca
'distribution'函数是一个自制函数,生成具有指定均值、方差、上限、下限和给定数量的随机数的高斯分布:
function distribution(va, mu, ul, ll, nvals)
multiplier=10;
x = mu + randi(multiplier*nvals,1)*sqrt(va); % Generate sufficient random numbers
idx = (ll <= x) & (x <= ul); % Extract the value in the given range [min max]
while sum(idx)<nvals
multiplier=multiplier+1;
x = mu + randi(multiplier*nvals,1)*sqrt(va); % Generate sufficient random numbers
idx = (ll <= x) & (x <= ul); % Extract the value in the given range [min max]
end
x = x(idx);
x = x(1:nvals); % Extract numbers
您只看到灰色屏幕,因为该错误不会导致 Psychtoolbox window(您已将其设置为灰色背景)关闭。要关闭屏幕并显示错误,请将代码包装在 try、catch 语句中,如果遇到错误,它将关闭屏幕并重新抛出错误。
例如(这里的CODE这个词只是一个占位符,而不是用你的实际代码替换它)
try
CODE
catch e
sca;
rethrow(e)
end
在这种情况下,如果在 try 语句的主体内遇到错误,它将转到 catch 语句,调用错误 e
。首先屏幕将用 sca
关闭,然后通过 rethrow
.
就您遇到的实际错误而言,我认为您当前的代码至少存在一些问题:
Psychtoolbox 中的 Shuffle 函数大写 - 'Shuffle' 和 有一个输入。在这里,您使用 'shuffle',并提供 三个独立的输入。
不是提供 'left'、'right' 和 'up' 作为单独的输入, 将它们组合成单个元胞数组的元素:
正在替换:
randpos = shuffle(left, right, up);
与:
randpos = Shuffle({left, right, up});
和 select 它们作为元胞数组的元素:
正在替换:
Screen('DrawText', num2str(seq1), white, randpos(1));
与:
Screen('DrawText', window, num2str(seq1), white, [], black, randpos{1}(1), randpos{1}(2));
在您定义的分布函数中,您没有包含输出参数。将您当前的第一个函数行替换为:
function x = distribution(va, mu, ul, ll, nvals)
没有输入参数的 KbStrokeWait
只检查连接的第一个键盘。例如,如果您在笔记本电脑上使用外部键盘,这可能会忽略按键。使用KbStrokeWait(-3)
将检查所有连接的设备。你的分布函数似乎要么没有收敛于一个解决方案,要么花了很长时间来确定一个解决方案,因为它似乎是随机生成值然后检查它是否足够所需的间隔。相反,我会从生成随机正常值的内置函数开始。例如,以下将从均值为 0.02 且方差为 0.126 的分布生成 5 个值。它没有实现检查所有生成的值是否在给定范围内,但是:
randn(1, 5) * sqrt(0.126) + 0.02;
您在调用 DrawText 时缺少 window 指针。
可能还有其他问题,但解决这些问题会让您入门。