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

29 ビュー (過去 30 日間)
Anthony Seda
Anthony Seda 2021 年 6 月 4 日
コメント済み: Walter Roberson 2021 年 6 月 5 日
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 件のコメント
J. Alex Lee
J. Alex Lee 2021 年 6 月 5 日
maybe key down versus key up can give you the granularity you want on behavior?
Walter Roberson
Walter Roberson 2021 年 6 月 5 日

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by