How to implement press and hold button for app designer?

64 visualizaciones (últimos 30 días)
Joe Hidler
Joe Hidler el 31 de En. de 2022
Comentada: Joe Hidler el 10 de Feb. de 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.

Respuestas (1)

Bala Tripura Bodapati
Bala Tripura Bodapati el 8 de Feb. de 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 comentario
Joe Hidler
Joe Hidler el 10 de Feb. de 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])

Iniciar sesión para comentar.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by