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

조회 수: 30 (최근 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?

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Call 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