# Scale Slider: how can I do it?

**URL:** <https://psychtoolbox.discourse.group/t/scale-slider-how-can-i-do-it/4650>\
**Category:** Programming Help\
**Created:** [October 13, 2022, 7:11am UTC](https://psychtoolbox.discourse.group/t/scale-slider-how-can-i-do-it/4650 "2022-10-13T07:11:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![clar.sto](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@clar.sto](https://psychtoolbox.discourse.group/u/clar.sto)\
**Post date:** [October 13, 2022, 7:11am UTC](https://psychtoolbox.discourse.group/t/scale-slider-how-can-i-do-it/4650/1 "2022-10-13T07:11:19Z")

</div>

Hi there,  
I’m new to PsychToolbox, and new to programming. I need to program an experiment, and have already programmed parts of it, but now I came to a point where I just don’t manage to get further.  
I am just wondering whether it is possible to create a Scale Slider in PTB.  
I want to measure the response of the subjects to a stimulus on a scale of 1 to 100 using a scale slider.  
I found some scripts on the Internet but they doesn’t work.  
Does anyone have a script on this?  
I’m using Matlab R2022a Update 4.  
Best,  
Clarissa.

---

<div class="post-metadata">

**Author:** ![DeborahA](https://yyz2.discourse-cdn.com/free1/user_avatar/psychtoolbox.discourse.group/deboraha/32/362_2.png) [@DeborahA](https://psychtoolbox.discourse.group/u/DeborahA)\
**Post date:** [October 27, 2022, 2:56am UTC](https://psychtoolbox.discourse.group/t/scale-slider-how-can-i-do-it/4650/2 "2022-10-27T02:56:51Z")

</div>

Hi Clarissa!

I wrote a script some time ago to do a scale slider. I haven’t used it in a while, but I think it should still work. If I were doing this nowadays I would make it a function, but I hope the basic style of it is helpful to you - I’ve commented everything.

```auto
%% Screen parameters. You'll probably want to call these at the top of your script
screenNumber = max(Screen('Screens'));
Screen ('Preference', 'SkipSyncTests', 1);
Screen ('Preference', 'VisualDebugLevel', 1);
PsychImaging('PrepareConfiguration');
PsychImaging('AddTask', 'General', 'NormalizedHighresColorRange'); % Make colours be from 0 to 1
[w, scrRect] = PsychImaging('OpenWindow', screenNumber, [1, 1,1]); 

[xCenter, yCenter] = RectCenter(scrRect);
slack = Screen('GetFlipInterval', w); 
vbl = Screen('Flip', w);

%% Parameters for your scale and text that you want
question = 'How structured was the stimulus? ';
lowerText = 'Not';
upperText = 'Very';
pixelsPerPress = 2;
waitframes = 1;
lineLength = 500; % pixels
halfLength = lineLength/2; 
divider = lineLength/10; % for a rating of 1:10

baseRect = [0 0 10 30]; % size of slider
LineX = xCenter;
LineY = yCenter;

rectColor = [0 0 0]; % color for slider
lineColor = [0 0 0]; % color for line
textColor = [0 0 0]; % color for text

Screen('TextFont',w, 'Helvetica'); % font parameters
Screen('TextSize',w, 32);
Screen('TextStyle', w, 0);

%% set up what keys you want to use
KbName('UnifyKeyNames');
RightKey = KbName('RightArrow');
LeftKey = KbName('LeftArrow');
ResponseKey = KbName('Space');
escapeKey = KbName('ESCAPE');

%% Actual bit that draws the scale
while KbCheck; end
while true
    [keyIsDown, secs, keyCode] = KbCheck;
    pressedKeys = find(keyCode);
    if pressedKeys == escapeKey
        break
    elseif keyCode(LeftKey)
        LineX = LineX - pixelsPerPress;
    elseif keyCode(RightKey)
        LineX = LineX + pixelsPerPress;
    elseif pressedKeys == ResponseKey
        StopPixel_M = ((LineX - xCenter) + halfLength)/divider; % for a rating of between 0 and 10. Tweak this as necessary.
        break;
    end
    if LineX < (xCenter-halfLength)
        LineX = (xCenter-halfLength);
    elseif LineX > (xCenter+halfLength)
        LineX = (xCenter+halfLength);
    end
    if LineY < 0
        LineY = 0;
    elseif LineY > (yCenter+10)
        LineY = (yCenter+10);
    end
    
    centeredRect = CenterRectOnPointd(baseRect, LineX, LineY);
  
    currentRating = ((LineX - xCenter) + halfLength)/divider; %
    ratingText = num2str(currentRating); % to make this display whole numbers, use "round(currentRating)"

    DrawFormattedText(w, ratingText ,'center', (yCenter-200), textColor, [],[],[],5); % display current rating 
    DrawFormattedText(w, question ,'center', (yCenter-100), textColor, [],[],[],5);
    
    Screen('DrawLine', w, lineColor, (xCenter+halfLength ), (yCenter),(xCenter-halfLength), (yCenter), 1);
    Screen('DrawLine', w, lineColor, (xCenter+halfLength ), (yCenter +10), (xCenter+halfLength), (yCenter-10), 1);
    Screen('DrawLine', w, lineColor, (xCenter-halfLength ), (yCenter+10), (xCenter- halfLength), (yCenter-10), 1);
    
    Screen('DrawText', w, lowerText, (xCenter-halfLength), (yCenter+25), textColor);
    Screen('DrawText', w, upperText , (xCenter+halfLength) , (yCenter+25), textColor);
    Screen('FillRect', w, rectColor, centeredRect);
    vbl = Screen('Flip', w, vbl + (waitframes - 0.5) * slack);
end

%% Close everything and display the rating. If you're doing this during a script, just put in a flip and save the rating somewhere. 

disp('Rating: ') ;
disp(StopPixel_M);
sca;
Screen('CloseAll');

```

Hope this is useful to you!

Cheers

Deborah.
