Get user's selection from GUI
17 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (1 件)
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
Thanks,
Kiran
3 件のコメント
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
参考
カテゴリ
Help Center および File Exchange で Interactive Control and Callbacks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!