close all;
clearvars;
Screen('Preference', 'SkipSyncTests', 1);
%% 初始化设置
PsychDefaultSetup(2);
screens = Screen('Screens');
screenNumber = max(screens);
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);
[w, rect] = PsychImaging('OpenWindow', screenNumber, black);
% 定义实验参数
ifi = Screen('GetFlipInterval', w);
time=0;
topPriorityLevel = MaxPriority(w);
Priority(topPriorityLevel);            %优先级设置为最高
textSize = 48;
textFont = 'Arial';
textColor = [255 255 255];
% 设置屏幕的左右两半
halfWidth = rect(3) / 2;
leftHalf = [0 rect(4)/5 rect(3)/4 rect(4)*4/5];%修改闪烁大小改这里,上下左右四条线
% rightHalf = [halfWidth 0 rect(3) rect(4)];
rightHalf = [rect(3)*3/4 rect(4)/5 rect(3) rect(4)*4/5];
% 设置文本位置(居中)
[textX1, textY1] = RectCenter(leftHalf);
[textX2, textY2] = RectCenter(rightHalf);
textX1 = textX1-60;
textX2 = textX2-60;
Screen('FillRect',w,255,leftHalf); 
Screen('FillRect',w,255,rightHalf);
Screen('TextSize',w,100);
Screen('DrawText',w,'Yes',textX1, textY1,textColor); 
Screen('DrawText',w,'No',textX2, textY2,textColor); 
while 1
    FrameNum=0;  %用来计算 帧,用来计算,没刷新一次,算一帧,i*刷新一帧的时间=sin的t
    if KbCheck
    %%%%%%%%%%%%%%%%%%%%%
        WaitSecs(0.1);
        waitframes = 1;
        vbl = Screen('Flip',w);    %vbl存的第一次刷新屏幕的时间
        while ~KbCheck   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%开始闪烁
            %[secs,keycode]=KbPressWait;
%             Screen('FrameRect',w,255,[15,30,13+9*jiangex+10*side,30+side],3);%%%%文本的输入框
%             Screen('FillRect',w,flicker(15.6,FrameNum,0.13*pi,ifi),[20+9*jiangex+9*side,33,10+9*jiangex+10*side,27+side]);
            %第一行方块
            Screen('FillRect',w,flicker(7.5,FrameNum,1.0*pi,ifi),leftHalf); 
            Screen('FillRect',w,flicker(10.0,FrameNum,1.0*pi,ifi),rightHalf); 
            FrameNum=FrameNum+1;
            vbl = Screen('Flip', w, vbl + (waitframes - 0.5) * ifi);  %把上次刷新的时间+1s后再刷新
        end   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%结束闪烁
        break;
    %%%%%%%%%%%%%%%%%%%%%
    end
end
Screen('CloseAll');

这段代码是一个基于Psychtoolbox的Matlab程序,用于创建一个屏幕并在屏幕上方块进行闪烁并显示'Yes'和'No'的文本提示。

代码功能:

  1. 初始化设置: 关闭所有图像窗口,清除变量,跳过同步测试,设置屏幕参数。
  2. 定义实验参数: 设置屏幕刷新间隔、颜色、字体等参数。
  3. 设置屏幕区域: 将屏幕分为左右两半,用于后续呈现不同的闪烁方块。
  4. 绘制文本: 在左右两侧分别绘制'Yes'和'No'文本。
  5. 闪烁循环: 使用 while 循环和 KbCheck 函数实时监测键盘输入,进入闪烁状态。
  6. 控制闪烁: 通过 Screen('FillRect',...) 函数改变方块颜色实现闪烁效果,并使用 Screen('Flip',...) 函数控制刷新频率。
  7. 结束程序: 当检测到键盘输入时,退出循环,关闭所有窗口。

代码亮点:

  • 使用 Priority(topPriorityLevel) 将程序优先级设置为最高,确保程序运行流畅。
  • 使用 Screen('Preference', 'SkipSyncTests', 1) 跳过同步测试,简化程序运行流程。
  • 使用 Screen('Flip',...) 函数精确控制屏幕刷新时间,实现精准的闪烁效果。
  • 使用注释清晰地解释代码功能,方便理解和学习。

这段代码可以作为学习 Psychtoolbox 的入门示例,帮助初学者了解如何使用该工具包创建简单的视觉刺激。

Matlab Psychtoolbox实现屏幕闪烁及文本显示

原文地址: https://www.cveoy.top/t/topic/lwF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录