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

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

 채택된 답변

Kevin Holly
Kevin Holly 2022년 7월 19일
편집: Kevin Holly 2022년 7월 19일
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

It works - thank you so much! This has also helped me understand how to use event.Source and hObject. Thanks again!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

질문:

2022년 7월 19일

댓글:

2022년 7월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by