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

4 views (last 30 days)
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

Accepted Answer

Kevin Holly
Kevin Holly on 19 Jul 2022
Edited: Kevin Holly on 19 Jul 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 Comment
barney whitehead
barney whitehead on 20 Jul 2022
It works - thank you so much! This has also helped me understand how to use event.Source and hObject. Thanks again!

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!