Is there a sleep() function in MATLAB similar to Python?

I am working on a code that asks a user for a keyboard input using the "a,z,k,m" keys and I want to be able to accept only the initial push of the button and have the code stop taking inputs for whatever key they pressed. The code will eventually be used in a gui to manipulate a two-arm pendulum so when the user presses the "a" key the first arm will rotate upwards and when they press the "z" key the arm will move downwards. The "k" and "m" keys will serve the same function but will manipulate the second arm.
I have tried the pause() and the uiwait command, but it does not seem to produce the result I am looking for. I have seen something similar to what I am looking for in the Python function sleep() which allows you to introduce a delay in the execution of your program.
Additionally, an adaptation of the sleep() function is time.sleep() and allows us to take an argument by inputting the (seconds) to halt or to suspend before it moves forward to the next step. This would allow the user to realize that holding the "a" key down would not manipulate the pendulum anymore.
function qOut = inputButtonPress(qOut)
global dt maxAngularAcc12 maxAngularAcc23 MassVector LengthVector nic nX
w = waitforbuttonpress;
if w
press = get(gcf, 'CurrentCharacter');
end
if press=='a' %Looking for a button press
disp('button is a')%Here for troublehsooting
q= qOut+ [0 0 0 0 0 0 0 maxAngularAcc12*dt 0 0] %Adds the max angular acceleration for the 12 hinge point to velocity change
qOut=simulation(LengthVector,MassVector,nic,nX,q,dt)
qOut=inputButtonPress(qOut); %loops the function
elseif press=='z' %Looking for z button press
disp('button is z')
q= qOut+ [0 0 0 0 0 0 0 -maxAngularAcc12*dt 0 0]
qOut=simulation(LengthVector,MassVector,nic,nX,q,dt)
qOut=inputButtonPress(qOut);
elseif press=='k' %Looking for k button press
disp('button is k')
q= qOut+ [0 0 0 0 0 0 0 0 0 maxAngularAcc23*dt]
qOut=simulation(LengthVector,MassVector,nic,nX,q,dt)
qOut=inputButtonPress(qOut);
elseif press=='m' %Looking for m button press
disp('button is m')
q= qOut+ [0 0 0 0 0 0 0 0 0 -maxAngularAcc23*dt]
qOut=simulation(LengthVector,MassVector,nic,nX,q,dt)
qOut=inputButtonPress(qOut);
elseif double(press)==27
disp('Exiting Simulation Loop')
else
disp('Unexpected Value Pressed');
qOut = inputButtonPress(qOut);
end
end

5 commentaires

Not totally clear to me what effect you're missing -- a
pause(delay)
in the action after the keypress will hold that section from executing for delay seconds.
A key feature ofwhat I am looking for is to allow only one impulse everytime a key is pressed. So no matter how long the pressing continues it would only accept the first key stroke.
The pause(delay) stops the program momentarilly, but we are looking for the code to stop "repeating" when the key stays depressed.
dpb
dpb le 4 Juin 2021
Modifié(e) : dpb le 5 Juin 2021
You have to handle that in the callback function to discard keys after the first.
Nothing in the Python sleep() function would be any different from MATLAB in that regards.
maybe key down versus key up can give you the granularity you want on behavior?

Connectez-vous pour commenter.

Réponses (0)

Catégories

Produits

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by