App Designer: Programmatically toggle between buttons

7 visualizzazioni (ultimi 30 giorni)
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

Risposte (1)

Brian Hannan
Brian Hannan il 3 Ago 2017
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

Categorie

Scopri di più su Develop Apps Using App Designer in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by