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

15 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Kevin Holly
Kevin Holly le 19 Juil 2022
Modifié(e) : Kevin Holly le 19 Juil 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 commentaire
barney whitehead
barney whitehead le 20 Juil 2022
It works - thank you so much! This has also helped me understand how to use event.Source and hObject. Thanks again!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by