Get user's selection from GUI

조회 수: 10 (최근 30일)
Amit Vurgaft
Amit Vurgaft 2020년 7월 21일
댓글: Amit Vurgaft 2020년 7월 25일
Hi all,
I wrote the following function "myui" for GUI in Matlab.
I want to get the user's selection using 4 radio-buttons, and to save it (1-4) in the parameter "Tcase". Then, after the user pushes the button "select", I want to send "Tcase" as an output of the function "myui".
I'm not sure how to use the callbacks so I deleted them from the code, to make the code run.
Trying to find a solution in Google didn't work.
Please help!
(Any additional comments to improve the code will be appricated as well)
function [TCase,Well]= myui
f= figure();
set(gcf,'Position',[200 100 1000 400]);
% radiobutton choice
T_but = uibuttongroup(f,'Visible','off', 'Position',[0 0 0.3 50],'selectionc',@cselection);
for i=1:4
c(i)= uicontrol(T_but,'Style', 'radiobutton', 'String',['A',num2str(i)],'Position',[30 350-30*i 100 30]);
end
set(T_but,'selectedO',[]) % Initially no selection.
T_but.Visible = 'on';
guidata(gcf,c);
% "Select" button
btn = uicontrol(f,'Style','pushbutton', 'String','Select','Position',[600, 20, 100, 22]);
function [TCase] = cselection(varargin)
ree = guidata(gcbf);
TCase=sum(cell2mat(get(ree,'value'))'.*(1:length(ree))) % Tried to get the case number in a too complex way
end
end

답변 (1개)

Kiran Felix Robert
Kiran Felix Robert 2020년 7월 24일
편집: Kiran Felix Robert 2020년 7월 24일
Hi Amit,
It is my understanding that you wish to create a GUI using uicontrols Radio-button and Push-button and obtain Radio-Button data when Push-Button is clicked. I also understand that you have a problem with using call backs and sharing data among call backs. The Documentation about call backs can be found here and the article about sharing data among call back is available here. The following gives you one example to create two Radio-Buttons and send the choice to output using the nested functions approach.
function out = GUI();
f = figure('Name','Choice');
C1 = uicontrol(f,'Style','radiobutton','String','Case 1');
C2 = uicontrol(f,'Style','radiobutton','String','Case 2');
C1.Position = [120 130 100 20];
C2.Position = [120 150 100 20];
P = uicontrol(f,'Style','pushbutton','String','Select');
C1.Callback = {@C1_cbk};
C2.Callback = {@C2_cbk};
P.Callback = {@P_cbk};
Case = struct('Out','');
function C1_cbk(~,~)
Case.Out = '1';
end
function C2_cbk(~,~)
Case.Out = '2';
end
function P_cbk(~,~,handles)
out = Case.Out;
end
end
Furthermore, you can refer to MATLAB app designer for more effective GUI design.
Thanks,
Kiran
  댓글 수: 3
Kiran Felix Robert
Kiran Felix Robert 2020년 7월 24일
편집: Kiran Felix Robert 2020년 7월 24일
Hi Amit,
The function throws an error because the variable out is not assigned until the callback function gets executed, I also hope that there would have been no problem with the execution of the functionality. Well if you want to get rid of the error, you can try assigning a dummy value to variable out in your second line, (this will get assigned as soon as you call the function)
function out=GUI();
out = ' '; %Assign a Dummy value
f = figure('Name','Choice');
C1 = uicontrol(f,'Style','radiobutton','String','Case 1');
C2 = uicontrol(f,'Style','radiobutton','String','Case 2');
C1.Position = [120 130 100 20];
C2.Position = [120 150 100 20];
P = uicontrol(f,'Style','pushbutton','String','Select');
C1.Callback = {@C1_cbk};
C2.Callback = {@C2_cbk};
P.Callback = {@P_cbk};
Case = struct('Out','');
function C1_cbk(~,~)
Case.Out = '1';
end
function C2_cbk(~,~)
Case.Out = '2';
end
function P_cbk(~,~,handles)
out = Case.Out;
end
end
Amit Vurgaft
Amit Vurgaft 2020년 7월 25일
Thank you again!
I modified a bit your code to solve the issue of sending the empty output value instead of the selected value:
What was actually missing is the "waiting time" of the function before it sends the output value.
Now the function sends the output only after user's selection :)
If someone is interested, it looks like this:
function out=GUI();
f = figure('Name','Choice');
C1 = uicontrol(f,'Style','radiobutton','String','Case 1');
C2 = uicontrol(f,'Style','radiobutton','String','Case 2');
C1.Position = [120 130 100 20];
C2.Position = [120 150 100 20];
P = uicontrol(f,'Style','pushbutton','String','Select');
C1.Callback = {@C1_cbk};
C2.Callback = {@C2_cbk};
P.Callback = {@P_cbk};
Case = struct('Out','');
function C1_cbk(~,~)
Case.Out = '1';
end
function C2_cbk(~,~)
Case.Out = '2';
end
function P_cbk(~,~,handles)
out = Case.Out;
close(f);
end
uiwait(f);
end

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

카테고리

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

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by