How to implement press and hold button for app designer?

30 vues (au cours des 30 derniers jours)
Joe Hidler
Joe Hidler le 31 Jan 2022
Commenté : Joe Hidler le 10 Fév 2022
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.

Réponses (1)

Bala Tripura Bodapati
Bala Tripura Bodapati le 8 Fév 2022
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
  1 commentaire
Joe Hidler
Joe Hidler le 10 Fév 2022
Hello Bala,
Unfortunately state buttons do not work for our application. We do not want to 'latch' a behavior, which would happen with the state button. With a press and hold, as soon as you release the button, the havior stops. In guide, the following functions work perfectly yet does not work with app designer (note button is set to inactive in guide):
function upbutton_ButtonDownFcn(hObject, eventdata, handles)
% switch button color and set up button function
set(gcf,'WindowButtonUpFcn',{@UpButtonUpFcn,handles});
set(handles.upbutton,'BackgroundColor',[128,255,255]/255,'ForegroundColor',[0,0,0])
%%% code to implement for button down %%%%
% Update handles structure
guidata(hObject, handles);
function UpButtonUpFcn(src,eventdata,handles)
%%% code to implement when button released %%%
set(handles.winchupbutton,'BackgroundColor',[0,64,128]/255,'ForegroundColor',[1,1,1])

Connectez-vous pour commenter.

Catégories

En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by