psychopy.org | Reference | Downloads | Github

Changing values for slider ticks and labels in loop

**Win10
**PsychoPy version 3.07

I want to create a loop in which the subject responds to questions via a slider.
However, the slider should have different ticks and labels for each iteration. I.e. for four of my five questions, it should be a scale from 1 to 7 with the labels ‘Strongly disagree’ and ‘Strongly agree’ at its outer ends, and the last one should have a scale with only 1 and 2 on it with the labels ‘no’ and ‘yes’. All the five questions shall be shown in random order, so all of them must be in the same loop.

I already tried to specify the values for ticks and labels in the conditions file, but that did not work Instead I got the following error message:

labels=[$labels], ticks=[$ticks],
^
SyntaxError: invalid syntax

Is there any other way to change labels and ticks in each iteration (which also should be compatible with Pavlovia)?

1 Like

Hi @visual.narrative, you can set the labels using a code component. Open a new code component, select “Both” for the code type, and in the right panel; for the JavaScript, where slider is the name of your slider.

slider.labels = ["up", "down"];  // or whatever labels you want
slider._needVertexUpdate = true;
slider._buildSlider();
1 Like

Thank you very much for your answer @dvbridges !

The code snippet you posted changes the slider labels for every iteration in the loop. However, I want to change the labels only in one out of five iterations.

For example: In a loop, four out of five randomized questions should be answered on a slider with labels ‘Strongly disagree’ and ‘Strongly agree’, while the fifth question should show a slider with the labels ‘False’ and ‘True’.

Is it possible to have different slider labels only for one iteration?

Yes, you can create a counter and change the iteration when the counter reaches 5 (or 4 for zero-indexed number):

// Begin Experiment
counter = 0;

// Begin routine
if (counter === 4) {
  slider.labels = ["True", "False"]; 
  counter = 0;  // reset slider counter
}  else {
  slider.labels = ["agree", "disagree"]; 
}

counter = counter + 1;
slider._needVertexUpdate = true;
slider._buildSlider();

1 Like

Thanks @dvbridges , that’s very helpful!

Just one more thing: I want to randomize the order of my questions, therefore to simply use a counter does not work for me. Instead, I’d like to write in the if-statement of the code you posted that the first three letters of the text field in a Text component should be “ABC”.

How do I access the text string of a Text component in java? The name of this Text component is “question1”.

Thanks in advance!

Ok. You can get the text from a text component using:

myText = question1.text;

Then, if you want to check that your text starts with ABC:

if (myText.startsWith("ABC")) {
  // do something
}

Worked out perfectly, thanks a lot!

**Win10
**PsychoPy version 2020.1.3

I’d want to change the slider settings so that a different couple of labels is displayed at every repeat of the routine. I did not fully understand the solutions proposed here (most of all the use of code component).
After creating a code component with this snippet

i receive this error in runner

slider._needVertexUpdate = true;
NameError: name ‘true’ is not defined

and i wonder if i’ve done any wrong move.

@SalParadise91, your error message looks like a Python error message, and the code above is JavaScript code for online use. So, make sure your code components are either using code type of “JS”, or “Both” where JS is in the right hand panel of the code component.

Thanks a lot and sorry for my newbieness!
I’d actually want my labels to change (their content, not their number) relating to a text component in the same routine.
I created a loop where a text component displays a different sentence (randomly) at every repeat, using a variable in a .xlsx file. I’d want the possible answers (the slider’s labels) to be taken from the sentence displayed.

For example, if the sentence is: “The horse raced past the barn fell”, and the question is: “Which is the main verb?”, the labels should be “raced” - “fell”.

Also, i’m planning to carry out part of the experiment online and part in presence (cause i need to record voices), so - if a solution is actually available - i need it to work in both the environments.

Thanks in advance!