How can I limit input into the command window?

I have written an experiment where once in a while I have to input some numbers (1 and 2) into the command window to make the experiment continue. I am currently doing this using the input() function. However, the participant also has access to an instrument which writes letters to matlab. I want to limit the characters I can write into the command window to only include the numbers, so that the instrument doesn't interfere with my input. Checking for the right input after its been given will not work, as the instrument can continuiously send letters, making it impossible for me to write in just my numbers.
I currently have these lines of code, where I manually input whether I want to continue or abort after each block of trials, after which my input is evaluated.
while 1
cont=input(sprintf("do you want to continue with block %g? (1 for yes, 2 for no and abort): ",wrun));
%%% if you want to continue, go to next iteration (end while loop)
if cont ==1
break
%%% if you want to abort, make stop variable (end while loop)
elseif cont ==2
stop=1;
break
end

 Respuesta aceptada

Star Strider
Star Strider el 25 de Mzo. de 2022

0 votos

I generally prefer to use the inputdlg fucntion, since it does not involve the Command Window.
If MATLAB is writing to the Command Window and that is causing problems using the input function, inputdlg could be a way to avoid that.

5 comentarios

Femke R
Femke R el 25 de Mzo. de 2022
Thanks for your reply! The problem is that the instrument the participant is holding writes letters to matlab in general (it is treated as a keyboard). So even in a pop-up window, the instrument could still write letters, interfering with the numbers I need to write. Is there a way with inputdlg to limit which keypresses actually make it into the window?
Rik
Rik el 25 de Mzo. de 2022
Then maybe you should consider a figure with a keypress function callback. That way you can forward the letters to anywhere you need, and you can catch the numbers for this loop.
Star Strider
Star Strider el 25 de Mzo. de 2022
My pleasure!
My answer is based on my understanding that MATLAB was writing to the Command Window and that was interfering with the input function. I have no idea what ‘the instrument’ is doing, or how to restrict its imputs to MATLAB, since I have no idea what it is or how it works.
The only other option I can suggest is questdlg since that requires clicking on a radio button for input. If there are a restricted number of possible inputs to it, that could potentially work. The listdlg function is another option to consider.
I obviously cannot test any of these, so this is my best guess.
Femke R
Femke R el 25 de Mzo. de 2022
Questdlg solves my problem, thank you!
Star Strider
Star Strider el 25 de Mzo. de 2022
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre App Building en Centro de ayuda y File Exchange.

Productos

Versión

R2020a

Preguntada:

el 25 de Mzo. de 2022

Comentada:

el 25 de Mzo. de 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by