Get user's selection from GUI

14 Ansichten (letzte 30 Tage)
Amit Vurgaft
Amit Vurgaft am 21 Jul. 2020
Kommentiert: Amit Vurgaft am 25 Jul. 2020
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

Antworten (1)

Kiran Felix Robert
Kiran Felix Robert am 24 Jul. 2020
Bearbeitet: Kiran Felix Robert am 24 Jul. 2020
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 Kommentare
Kiran Felix Robert
Kiran Felix Robert am 24 Jul. 2020
Bearbeitet: Kiran Felix Robert am 24 Jul. 2020
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 am 25 Jul. 2020
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by