How can I assign a value to variable using buttons?

44 views (last 30 days)
Hello, I'm fairly new to scripting and I'm working on a project where a user has to select a specific bolt. If it were just 2 or 3, I wouldn't bother with buttons but it will be around 20 different specific bolt types. I'm not sure how I can creat a button that assigns a value to a certain variable and then continues on with the rest of the script. This is a simple test I tried in order to get the command when to print a statement once button 3 was pressed. I know there are different types of buttons/switch so maybe I'm using the wrong one. Any advice would be much appreciated.
%Collection of bolt specification
fig = uifigure;
Bolt1 = uibutton(fig,'State','Text', 'Bolt 1','Value', false,'Position',[50,380, 100, 22]);
Bolt2 = uibutton(fig,'State','Text', 'Bolt 2','Value', false,'Position',[50,350, 100, 22]);
Bolt3 = uibutton(fig,'State','Text', 'Bolt 3','Value', false,'Position',[50,320, 100, 22]);
Bolt4 = uibutton(fig,'State','Text', 'Bolt 4','Value', false,'Position',[50,290, 100, 22]);
Bolt5 = uibutton(fig,'State','Text', 'Bolt 5','Value', false,'Position',[50,260, 100, 22]);
Bolt6 = uibutton(fig,'State','Text', 'Bolt 6','Value', false,'Position',[50,230, 100, 22]);
button_state = get(Bolt3,'Value');
if button_state == 1;
disp('This works');
end
  3 Comments
Stephen23
Stephen23 on 7 Oct 2020
Edited: Stephen23 on 7 Oct 2020
"'I'm not sure how I can creat a button that assigns a value to a certain variable and then continues on with the rest of the script."
GUIs are inherently based on asychronous code, in particular based on event triggers and timers that run code (e.g. callback functions). Scripts use linear, synchronous code, so the two are really apples and oranges. You can certainly write a callback function assigns a value to some variable, but you will really need to consider how that value (which exists inside the triggered callback function at some random moment in time) will be used by linear script (simple solution: call the script/function from the callback, essentially make it part of the GUI).
Instead of thinking of the buttons as being part of a script, a much better approach is to consider the entire GUI as performing the required operations on your data: importing, processing, plotting, saving. Everything happens inside the GUI:
Highly recommended:
Note: of course you should use a loop or similar to create the buttons, and definitely do NOT use numbered variable names!

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 7 Oct 2020
Edited: Stephen23 on 7 Oct 2020
I think writing your own GUI is a red herring. Unless it really is your goal to learn all about asynchronous code, callback functions, etc., then you are better off concetrating on the rest of your script. Writing a GUI is an entire project in itself.
Instead you can simply use one of MATLAB's inbuilt dialog boxes to let the user select from twenty buttons:
A simple working example (I selected '"Blue" in the dialog box):
>> C = {'Red','Yellow','Blue','Green','Orange','Purple'};
>> P = 'Select a bolt color';
>> X = listdlg('ListString',C, 'SelectionMode','single', 'PromptString',P);
>> C{X}
ans =
Blue
  1 Comment
Jackson Smith
Jackson Smith on 7 Oct 2020
Thank you so much! This is alot simpler and more for what I was after.

Sign in to comment.

More Answers (1)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam on 7 Oct 2020
fig = uifigure;
Bolt1 = uibutton(fig,'Text', 'Bolt 1','Position',[50,380, 100, 22],'ButtonPushedFcn', 'button_state=1;button_state');
Bolt2 = uibutton(fig,'Text', 'Bolt 2','Position',[50,350, 100, 22],'ButtonPushedFcn', 'button_state=2;button_state');
Bolt3 = uibutton(fig,'Text', 'Bolt 3','Position',[50,320, 100, 22],'ButtonPushedFcn', 'button_state=3;button_state');
Bolt4 = uibutton(fig,'Text', 'Bolt 4','Position',[50,290, 100, 22],'ButtonPushedFcn', 'button_state=4;button_state');
Bolt5 = uibutton(fig,'Text', 'Bolt 5','Position',[50,260, 100, 22],'ButtonPushedFcn', 'button_state=5;button_state');
Bolt6 = uibutton(fig,'Text', 'Bolt 6','Position',[50,230, 100, 22],'ButtonPushedFcn', 'button_state=6;button_state');

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!