円の中央に文字列を描画する方法です。
Screen('TextBounds')
を使用すると、スクリーン上で文字列が占める長方形領域を知ることができます。
これを利用すると、図形の中心と、文字列の中心とを一致させることができます。
Screen('TextBounds')
を呼び出す前にフォントの種類、サイズ、描画文字列を設定しておくことを忘れないようにしてください。
%画面の中央に円と文字列を描画するfunction TextPosCheckListenChar(2);AssertOpenGL;KbName('UnifyKeyNames');%myKeyCheck;try screenNumber=max(Screen('Screens')); %ウィンドウでの呈示 %[windowPtr, windowRect]=Screen('OpenWindow', screenNumber, [0 0 0], [100, 200, 700, 600]); %フルスクリーンでの呈示 [windowPtr, windowRect]=Screen('OpenWindow', screenNumber, [0 0 0]); %画面の中央の座標 [centerX centerY] = RectCenter(windowRect); %赤い円の描画 Radius = 30; %円の半径 tmpRect = [centerX - Radius, centerY - Radius, centerX + Radius, centerY + Radius] Screen('FrameOval', windowPtr, [255 0 0], tmpRect); %描画する文字列 Screen('TextFont',windowPtr, 'Courier New'); % フォントの種類によっては中央に描画できないことがあります。 Screen('TextSize',windowPtr, 30); teststr = 'ab'; %スクリーン上で文字列が占める長方形領域が normBoundsRect %事前にフォントの設定を済ませておくこと。 [normBoundsRect, offsetBoundsRect]= Screen('TextBounds', windowPtr, teststr); %動作確認のためにnormBoundsRectを緑で描画してみましょう。 tmpRect = CenterRectOnPoint(normBoundsRect, centerX, centerY); Screen('FrameRect', windowPtr, [0 255 0], tmpRect); %文字列の描画(赤い円と緑の四角の中央に描画されるでしょうか) startX = centerX - normBoundsRect(RectRight)/2; startY = centerY - normBoundsRect(RectBottom)/2; DrawFormattedText(windowPtr, teststr, startX, startY, [255 255 255]); Screen('Flip', windowPtr); KbWait; %終了処理 Screen('CloseAll'); ShowCursor; ListenChar(0); catch %終了処理 Screen('CloseAll'); ShowCursor; ListenChar(0); psychrethrow(psychlasterror);end;