Integrating a touchscreen in a Psychtoolbox script (MATLAB)

have this very simple script in MATLAB. It draws a square in the middle of the screen and waits for the user response, provided by pressing the spacebar. Once given the response, the script presents the square again…and so on.

I want to integrate a touchscreen response, so that the user can respond touching the square on a touchscreen.

I know it possible to do it by using some function in MultiTouchDemo: Psychtoolbox-3 - MultiTouchDemo

But I really cannot integrate it in my script. Any advice?

This is my script:

try
    screens = Screen('Screens');
    whichscreen = max(screens);
    [myscreen,rect]=Screen('OpenWindow',whichscreen);
    DrawFormattedText(myscreen, 'PRESS ANY KEY TO PROCEED', 'center', 'center');
    Screen('Flip', myscreen);
    KbWait;
    space = KbName('space');
    ntrials = 6;
    colorsequence = [1, 2, 2, 2, 1, 1];
    rt = zeros(ntrials, 1);
    stimulus_duration = zeros(ntrials, 1);
    for i=1:ntrials
        WaitSecs(1);
        Screen('FrameOval', myscreen, 0, CenterRect([0, 0, 10, 10], rect));
        onset_fixation = Screen('Flip', myscreen);
        if colorsequence(i) == 1
            stimuluscolor = [255, 0, 0];
        else
            stimuluscolor = [0, 255, 0];
        end
        Screen('FillRect', myscreen, stimuluscolor, CenterRect([0, 0, 50, 50], ...
            rect));
        random_delay = rand+0.5;
        onset_stimulus = Screen('Flip', myscreen, onset_fixation + random_delay);
        t0 = GetSecs;
        [keyIsDown, secs, keyCode] = KbCheck;
        while keyCode(space)==0 && (secs - onset_stimulus) < 2
            [keyIsDown, secs, keyCode] = KbCheck;
        end
        rt(i) = secs - t0;
        stimulus_offsettime = Screen('Flip', myscreen);
        stimulus_duration(i) = stimulus_offsettime - t0;
    end
    screen('CloseAll');
catch
    Screen('CloseAll')
    rethrow(lasterror)
end

Thank you, best regards

Have you read the source code for that demo? You are missing all the commands that find, open and retrieve data from the touch device in your code! Read help TouchInput to understand what you must do; to summarise that documentation:

  1. Find the device(s) to use for touch input via GetTouchDeviceIndices().
  2. Get basic information about a device of choice via GetTouchDeviceInfo().
  3. Create (TouchQueueCreate()) a touch input queue for the selected device.
  4. Use TouchQueueStart(), TouchQueueStop() and TouchEventFlush() to start,
    and stop data collection, or to discard all currently pending touch input.
  5. Use TouchEventAvail() to find the number of collected touch events, and
    TouchEventGet() to retrieve events. These events provide all the basic
    information of interest: The time when the event was received, the (x,y)
    touch coordinates in different useful coordinate systems, if it was a
    start of a new touch, the end of an old touch, or some state change on an
    existing touch, e.g., position, pressure, shape etc. Additional device
    and operating system specific info (non-portable!) can be retrieved via
    the helper function GetTouchValuators().