App Designer: Programmatically toggle between buttons

15 次查看(过去 30 天)
Hello,
I have a toggle button group with 5 buttons (A, B, C, D etc...). I would like to have another "sequence" button which when pressed, changes the toggle button group from button A pressed (value=1) to button B pressed (value=1) and so on. Is there a way to implement this? I know how to explicitly press one toggle button by changing its value to 1. But I would like the "sequence" button to always press the next button. Thank you
Best

回答(1 个)

Brian Hannan
Brian Hannan 2017-8-3
You can do this using the Buttons, OldValue, and NewValue properties described here. If you have a button group where the last button is named "next", the following callback will allow you to cyclically toggle the other buttons.
This code checks whether the "next" button has been selected. If so, It finds the index of the previously selected button, identifies the next button down, and sets its value to true.
function ButtonGroupSelectionChanged(app, event)
if strcmp(event.NewValue.Text, 'next')
buttonNameCell = {app.ButtonGroup.Buttons(1:end-1).Text};
lastSelectionIx = find(strcmp(buttonNameCell, event.OldValue.Text));
numButtons = numel(app.ButtonGroup.Buttons);
nextButtonIx = mod(lastSelectionIx, numButtons-1) + 1;
isButtonPressedArray = false(1:numButtons);
isButtonPressedArray(nextButtonIx) = true;
for k = 1:numButtons
app.ButtonGroup.Buttons(k).Value = isButtonPressedArray(k);
end
end
end

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by