How can i set a time for "input" waiting?

I have:
a=input('>>','s')
and MatLab waits for my input information till the infinity, but I need it to wait only first 2 seconds, if no input arguments - ignore "input" comand.
I need you help!

Answers (3)

Here is a solution based on timer callbacks and simulated keypresses.
  • The following function will simulate an Enter keypress whenever called. Save this function on MATLAB path
function pressEnter(HObj, event)
import java.awt.*;
import java.awt.event.*;
rob = Robot;
rob.keyPress(KeyEvent.VK_ENTER)
rob.keyRelease(KeyEvent.VK_ENTER)
end
  • Then in your code, attach the pressEnter function to the timerFcn of a timer. For example
t = timer('ExecutionMode', 'singleShot', 'StartDelay', 5, 'TimerFcn', @pressEnter);
start(t);
x = input('Enter:', 's');
stop(t);
delete(t);
This code will start a single shot timer and after 5 seconds, it will simulate an Enter keypress and stop the blocking input command. This, however, has a disadvantage that no matter what user types, the Enter key press will be simulated after 5 seconds, therefore user input will be interrupted.

8 Comments

Anders Olsen
Anders Olsen on 28 May 2018
Edited: Anders Olsen on 28 May 2018
Thanks for the input, but I can't get this to work.
When I use your example code for the timer, once I execute the script, it waits 5 seconds to simulate the key press. But the simulated key press is not compatible with the input-function, so if I give it an input and press Enter before the 5 second mark, an additional Enter key press is just simulated 5 seconds after executing the script. If I wait more than 5 seconds to give it an input, then it waits until I give it an input, at which point it immediately simulates an additional Enter key press.
The fact that any user input would be interrupted after the specified time is not a problem.
For your first point. That even if you press enter after typing input before 5 seconds, then it still simulate an enter keypress. Have you added the lines
stop(t);
delete(t);
immediately after the input statement. Because it will delete the timer object and it should not be able to simulate the keypress.
For your second point, I am not able to replicate the same problem. When I run it and don't enter anything for 5 seconds, it automatically terminates the input command. Note for this to happen, you must bring the focus to command window. If you run the script by pressing Run button then it can take the focus away from the command window. A simple solution to automatically bring command window to focus id by adding the following line before the input statement
commandwindow
Your first suggestion, adding the stop and delete statements, works like a charm. I thought I had them included, but apparently I didn't.
Right now I'm using the function you gave me, with the following snippet of code:
t = timer('ExecutionMode', 'singleShot', 'StartDelay', 5, 'TimerFcn', @pressEnter);
start(t);
commandwindow
x = input('Enter:', 's');
stop(t);
delete(t);
This still just hangs on "busy", until I press enter manually, at which point it adds the simulated Enter key press.
I'm running the script with F5, and can verify that the focus shifts to the command window, when I run it.
That's strange. I don't get this issue. I get the following behavior. I am not pressing any key in this animation.
You can try to modify your callback function like this to see if it really executed after 5 seconds
function pressEnter(HObj, event)
import java.awt.*;
import java.awt.event.*;
rob = Robot;
rob.keyPress(KeyEvent.VK_ENTER)
rob.keyRelease(KeyEvent.VK_ENTER)
disp('Hello world'); %<--- Add this line
end
Now I have modified the pressEnter function to match yours. Here's what's happening when I run it. As you can see, I'm running the code, waiting for 5+ seconds, then giving it an input. Only then does the function execute.
The function that is called is:
function pressEnter(HObj, event)
import java.awt.*;
import java.awt.event.*;
rob = Robot;
rob.keyPress(KeyEvent.VK_ENTER)
rob.keyRelease(KeyEvent.VK_ENTER)
disp('Hello world');
end
I could not understand the issue. I tried it on both windows and OS X and it is working in both cases. In your case, it appear that something is blocking the timer from executing. It might be issue with MATLAB version. I tested it on R2017b and R2018a.
Anders Olsen
Anders Olsen on 29 May 2018
Edited: Anders Olsen on 29 May 2018
Alright. I'm using R2016a, so that might be it. But thanks a lot for the suggestion! Maybe I'll get it to work at some point.

Sign in to comment.

I'm not sure if there is a direct way.
You can do it in a slightly different way.
tic
disp('Press space to enter what you need to before 5 sec passes')
pause(5)
v = toc;
if v < 5
disp('space got pressed in time.')
a=input('>>','s');
else
disp('space wasn''t pressed')
end
It means that the program doesn't pause for too long, and gives the user as long as they need to enter what they need to.

3 Comments

Thanks a lot! with some modifications but your method works!!!
Oh idea with tic tac was brilliant, but pause not stops by space, here is my code:
for i=1:500
tic pause(1) v=toc; if v<.9 %several statements end
I don't see how this would work. As Nazariy points out, specifying a time argument for pause, removes the ability to break the pause with a keypress.

Sign in to comment.

Jan
Jan on 28 May 2018
Using the command window and timer is a good idea. But it is much easier, if you do not use the command window, but a GUI with a timer. Then you do not have to emulate a pressed Enter key, but can request the current state of an edit field. Note that the 'String' property is not updated during the editing, but only if Enter was pressed or another GUI element is selected.
Is a GUI a working alternative for you?

Categories

Asked:

on 25 Feb 2014

Edited:

on 29 May 2018

Community Treasure Hunt

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

Start Hunting!