How to implement press and hold button for app designer?
30 次查看(过去 30 天)
显示 更早的评论
I'm trying to implement a button where the button toggles as long as I press and hold the button (and disengages when I let go). With older Matlab versions, you could use functions like 'ButtonDownFcn' and 'WindowButtonUpFcn' to achieve this, but I'm now having a hard time with Matlab 2020a.
0 个评论
回答(1 个)
Bala Tripura Bodapati
2022-2-8
Hi Joe Hidler,
It is my understanding that you are not able implement a press and hold button for app designer, which toggles as along as it is pressed and held and disengages when released.
Currently there isn't a Button to specifically press and hold in the app designer. This issue is known, and the concerned parties may be investigating further. There is a possible workaround that can perform the similar functionality you want.
You can find "State Button" in the Component Library of the app browser. Refer here for more information:
and:
Clicking the state button once will turn the button "ON" until you click it again. So instead of holding and releasing you are clicking it once to effectively hold and clicking it again to effectively release the button. You can access the state of this button (1 i.e. true or 0 i.e. false) in other callbacks and functions to modify their behavior. This can be accessed by:
>> app.Button.Value (will be 1 i.e. true or 0 i.e. false)
% Note ('Button' has to be replaced by what you name this state button)
If you want to repeatedly run some code until you deactivate this button you can do that in its callback function. In the Design View, right click on the button and hover on Callbacks. There will be an option of "Add ValueChangedFcn callback". In that you can wrap your code in a while loop like:
function ButtonValueChanged(app, event)
while app.Button.Value % Replace 'Button' with what you name it
% Insert the code you want to run while the button is ON
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!