Is there a sleep() function in MATLAB similar to Python?
29 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
J. Alex Lee
le 5 Juin 2021
maybe key down versus key up can give you the granularity you want on behavior?
Walter Roberson
le 5 Juin 2021
Interruptable off, Busy Action 'cancel' ?
Réponses (0)
Voir également
Catégories
En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!