How to create a callback for radio buttons

101 次查看(过去 30 天)
I have a group of radio buttons with 3 choices (app.mode1, app.mode2, and app.mode3) in app-designer. When a user clicks one of the radio buttons, I want the name of a separate button to be changed to something else. For example: mode 1: "load", mode 2: "plot", mode 3: "display". I'm able to create a call back for the group of radio buttons as below:
function modeSelectionChanged(app, event)
selectedButton = app.mode.SelectedObject;
end
end
But I have no idea as to how to write the following codes. Would someone please help me out?
Many thanks!

采纳的回答

Cris LaPierre
Cris LaPierre 2021-3-16
编辑:Cris LaPierre 2021-3-16
Assuming your button has the name app.Button, and your radio buttons are labeled 'Mode1', 'Mode2', and 'Mode3', something like this
% Selection changed function: Mode
function ModeSelectionChanged(app, event)
selectedButton = app.Mode.SelectedObject;
switch selectedButton.Text
case 'Mode1'
app.Button.Text = "Load";
case 'Mode2'
app.Button.Text = "Plot";
case 'Mode3'
app.Button.Text = "Display";
end
end
  3 个评论
Cris LaPierre
Cris LaPierre 2021-3-16
What are you expecting it to do?
When I tested it, it changes the displayed text on app.Button based on the selection of the radio buttons. Again, you must label your buttons 'Mode1', etc for it to work.
You can make it a little more generic if you use the Value property instead
% Selection changed function: Mode
function ModeSelectionChanged(app, event)
selectedButton = app.Mode.SelectedObject;
switch selectedButton.Value
case 1
app.Button.Text = "Load";
case 2
app.Button.Text = "Plot";
case 3
app.Button.Text = "Display";
end
end
Leon
Leon 2021-3-16
That was indeed the problem!
I had the previous issue because I was using selectedButton.Text, but in my cases, they were the Values. Now it is working.
Many thanks!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by