App Designer: Radio buttons that prompt User input (Novice)

11 visualizaciones (últimos 30 días)
Hia Ghosh
Hia Ghosh el 6 de Ag. de 2019
Editada: Adam Danz el 12 de Ag. de 2019
In my data, each object is scored compared to a reference (range 0-1). In the GUI, the user will be given three options: use no threshold, only select objects meeting a score threshold, and select top X% of scores. If they choose a threshold or top X% I want the user to specify a value 0-1 for the threshold or percentage. If I use radio buttons for these three options, how can I prompt/allow user input?

Respuestas (1)

Adam Danz
Adam Danz el 6 de Ag. de 2019
Editada: Adam Danz el 12 de Ag. de 2019
In the "selection changed" callback function to the radio buttons group, you can ask the user for more information using the inputdlg(). This example below asks for more information if the button named "button2" was selected.
% Selection changed function: ButtonGroup
function ButtonGroupSelectionChanged(app, event)
selectedButton = app.ButtonGroup.SelectedObject;
if strcmpi(selectedButton.Text,'Button2') % If button2 was selected...
answer = inputdlg('Specify value between 0:1','More info needed');
answer = str2double(answer{:});
end
end
***See comments below for better strategies that do not rely on hard coded strings.
  5 comentarios
Steven Lord
Steven Lord el 12 de Ag. de 2019
Rather than checking the text of the selected button to determine if it is the one that should enable the edit box, I would directly check the object. This way your code doesn't need to change if you decide you want to adjust the wording or if you need to translate the labels to a different language. The latter is probably not all that common a workflow, but the former is.
The SelectionChangedFcn receives some information that will help you do this. Something like this should work, though I haven't tested it. ThresholdPercentage is the edit box in which the user can enter their threshold / percentage and UserSpecifiedPercentage is the radio button that should enable that box.
app.ThresholdPercentage.Enabled = ...
event.NewValue == app.UserSpecifiedPercentage;
For a little more self-explanatory (but slightly more verbose) code, store event.NewValue in a variable, something like buttonUserSelected.
buttonUserSelected = event.NewValue;
app.ThresholdPercentage.Enabled = ...
buttonUserSelected == app.UserSpecifiedPercentage;
Adam Danz
Adam Danz el 12 de Ag. de 2019
Editada: Adam Danz el 12 de Ag. de 2019
Good point. I'll have to tinker around with that.
Here's another alternative that does not rely on hard coded string names.
% Determine if button 2 was pressed.
if find(ismember(get(app.ButtonGroup.Buttons,'Text'),event.NewValue.Text)) == 2
I really wish there were a buttongroup property that just returns the integer index of the selected button (in the order that the buttons were created). It seems obvious to have that.

Iniciar sesión para comentar.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by