Initialising buttons so that the button text changes when said button is pressed

17 visualizaciones (últimos 30 días)
How can I give a set of state buttons the same callback to change the text on the button that is pressed? I have 10 buttons that say "connect" and when I click one, I want it to say "connecting". I could do this by creating a personalised callback function for each button, but how can I do it by creating one function that all buttons can use? I currently have this but hObject is definitely not working as I intended.
function Connect(app, hObject)
if hObject == true
TotalValueCheck(app)
% function for receivers to intercept signal is executed
% while:
hObject.Text = 'Connecting...';
%elseif signal detected from rover = true
% app.ConnectButton_1.Text = 'Connected'
elseif hObject == false
hObject.Text = 'Connect';
end
end

Respuesta aceptada

Kevin Holly
Kevin Holly el 19 de Jul. de 2022
Editada: Kevin Holly el 19 de Jul. de 2022
Barney,
Please see app attached. I added a public function as such:
methods (Access = public)
function func(app,hObject)
if hObject.Value==1
hObject.Text = 'Connecting...';
else
hObject.Text = 'Connect';
end
end
end
Then I added callback functoins referencing it.
% Value changed function: Button
function ButtonValueChanged(app, event)
func(app,event.Source)
end
% Value changed function: Button2
function Button2ValueChanged(app, event)
func(app,event.Source)
end
% Value changed function: Button3
function Button3ValueChanged(app, event)
func(app,event.Source)
end
% Value changed function: Button4
function Button4ValueChanged(app, event)
func(app,event.Source)
end
% Value changed function: Button5
function Button5ValueChanged(app, event)
func(app,event.Source)
end
% Value changed function: Button6
function Button6ValueChanged(app, event)
func(app,event.Source)
end
% Value changed function: Button7
function Button7ValueChanged(app, event)
func(app,event.Source)
end
% Value changed function: Button8
function Button8ValueChanged(app, event)
func(app,event.Source)
end
% Value changed function: Button9
function Button9ValueChanged(app, event)
func(app,event.Source)
end
% Value changed function: Button10
function Button10ValueChanged(app, event)
func(app,event.Source)
end
  1 comentario
barney whitehead
barney whitehead el 20 de Jul. de 2022
It works - thank you so much! This has also helped me understand how to use event.Source and hObject. Thanks again!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by