How do I get a user to choose an option by button?

1 view (last 30 days)
Jonathan Charron
Jonathan Charron on 10 Aug 2021
Commented: Rik on 12 Aug 2021
I'm getting very confused with AppDesigner. I just have a snippet-sized function I am writing, string = function(i), that converts a number selected, 1-7, into a corresponding string: eg. if i == 1, function output = "Apples" etc.
If a user runs the function by passing 1~7 into it, it will just return that string. However, if they pass in 0 I want the program to prompt the user for the input, and show them a list of the options (eg. fruit), and once they've chosen, change the value of i to 1~7. What do I have to do in AppDesigner to make buttons work like that?
  1 Comment
Rik
Rik on 12 Aug 2021
Why does it have to be 1 function? Just write a function that returns the list of options. Then you can use two other functions: one to select a fruit, the other to show the list to the user.
A function should do 1 Thing. If it does multiple Things you should probably split it.

Sign in to comment.

Answers (1)

Thomas Fournier
Thomas Fournier on 11 Aug 2021
Not sure of what you want to do but you can try to play with callbcak and "Visible" parameter of some component.
For exemple you create an edit field, the user will input a number between 0 and 7 in it.
you create a callback for this input (right clic, callback, add value changed fcn), this piece of code will be executed every time the input value change.
function EditField_2ValueChanged(app, event)
value = app.EditField_2.Value;
end
you can then create a new option list that is visible only if the input value is zero, for exemple if you created a Radio group button:
function EditField_2ValueChanged(app, event)
value = app.EditField_2.Value;
if value==0
app.ButtonGroup.Visible='on'
else
app.ButtonGroup.Visible='off'
end
end
  1 Comment
Jonathan Charron
Jonathan Charron on 12 Aug 2021
Very basically, I want my function to do this.
In main.m:
fruit_name = fruitchoice(1); % fruit_name = 'apple'
fruit_name = fruitchoice(2); % fruit_name = 'orange' etc.
But if I run main.m with
fruit_name = fruitchoice(0);
Instead of loading the value and just carrying on with main.m, that would run main.m by popping up a fruit menu from within fruitchoice, for the user to click on which fruit they want, from a list of buttons. Upon clicking that button, the menu would close, fruit_name would be loaded with the corresponding string, and main.m will continue. I'm just getting fairly lost about how to generate that panel and get it to pass back the numerical choice, eg. press option 1, get i=1 back, in fruitchoice, and have fruitchoice pass back fruit_name.
I want that functionality for a more complex multidiscipline development, where another dev has to run this function to load that string in, so it can be checked by numerous other functions: it helps the dev do optimization techniques because they can loop-through 1~7 etc for genetic algorithms, or they can just manually set 1~7 in their call to the function if they know what they're going to want for that run or block of code. Fruit_name and fruitchoice might have to be called any number of times.
It's not fruit but I can't be specific about what I'm working on, TLDR though, it's a short list of names for options that get passed back as a string, analagous to the fruit mentioned here.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by