How can I assign a value to variable using buttons?

조회 수: 31 (최근 30일)
Jackson Smith
Jackson Smith 2020년 10월 7일
답변: Asad (Mehrzad) Khoddam 2020년 10월 7일
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
Jackson Smith
Jackson Smith 2020년 10월 7일
Yes, thats exactly it!
Stephen23
Stephen23 2020년 10월 7일
편집: Stephen23 2020년 10월 7일
"'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!

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2020년 10월 7일
편집: Stephen23 2020년 10월 7일
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
Jackson Smith
Jackson Smith 2020년 10월 7일
Thank you so much! This is alot simpler and more for what I was after.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020년 10월 7일
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');

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by