Getting outputs from a uifigure when a button is pressed
Show older comments
Hello, I've gotten very stuck trying to make a uifigure that you can enter inputs into that will then be saved. My function to make this figure is:
function [str1,str2,cancel] = Input_categories(details)
gui_fig = uifigure('position',[745 420 430 240]);
opt_list = {'Option 1','Option 2','Option 3'};
opt_list2 = {'Option 1','Option 2','Option 3'};
uilabel(gui_fig,'text',char(details),'Position',[62 203 400 22]);
uilabel(gui_fig,'text','Sub-category','Position',[62 102 331 22]);
field1 = uidropdown(gui_fig,'Items',opt_list,'Editable','on','Position',[64 143 329 22]);
uilabel(gui_fig,'text','Category','Position',[62 164 331 22]);
field2 = uidropdown(gui_fig,'Items',opt_list2,'Editable','on','Position',[64 81 329 22]);
uibutton(gui_fig,'push','Position',[64 32 100 22],'Text','Ok','ButtonPushedFcn',@ok_fun);
uibutton(gui_fig,'push','Position',[293 32 100 22],'Text','Cancel','ButtonPushedFcn',@cancel_fun);
function ok_fun(~,~)
disp('ahh')
str1 = field1.Value;
str2 = field2.Value;
closereq()
end
function cancel_fun(~,~)
disp('cancel')
cancel = 1;
closereq()
end
end
The problem I'm running into is that I cant figure out how I'm supposed to get the data for the two uidropdown inputs out when the ok button is pressed. What I want to happen is that if you press the ok button then it will save out the inputs and if cancel is pressed it will cause a break in the main code (where the function is called) which i why im trying to have it output a cancel variable.
Im pretty new to matlab so I definately lack understanding of how it's all working. I've been trying to read the documentation and other similar questions but I just can't seem to wrap my head around it so any help is apreciated.
Cheers.
Accepted Answer
More Answers (1)
VBBV
on 13 Dec 2022
field1 = uidropdown(gui_fig,'Items',opt_list,'Editable','on','Position',[64 143 329 22],'Value','Text1');
uilabel(gui_fig,'text','Category','Position',[62 164 331 22]);
field2 = uidropdown(gui_fig,'Items',opt_list2,'Editable','on','Position',[64 81 329 22],'Value','Text2');
5 Comments
VBBV
on 13 Dec 2022
Use Value argument in the uidropdown function. It seems missing in you case
Toby Beisly
on 13 Dec 2022
VBBV
on 13 Dec 2022
function [str1 str2] = ok_fun(~,~)
disp('ahh')
str1 = field1.Value;
str2 = field2.Value;
closereq()
end
function cancel = cancel_fun(~,~)
disp('cancel')
cancel = 1;
closereq()
end
The functions ok_fun and cancel _fun are not returning any outputs. That's why you get error
Toby Beisly
on 13 Dec 2022
Output the arguments, field1, field2 instead of str1 str2 and see if the below code works accordingly. Use the ValueChangedFcn property in uidropdown since to record input data the return values from functions ok_fun need to be tracked using field1, field2 and same need to be passed as output arguments instead of str1 and str2
function [field1,field2,cancel] = Input_categories
details = 'xyz'
% str1 = field1
% str2 = field2
cancel = 0;
gui_fig = uifigure('position',[745 420 430 240]);
opt_list1 = {'Option 1','Option 2','Option 3'};
opt_list2 = {'Option 1','Option 2','Option 3'};
uilabel(gui_fig,'text',char(details),'Position',[62 203 400 22]);
uilabel(gui_fig,'text','Sub-category','Position',[62 102 331 22]);
uilabel(gui_fig,'text','Category','Position',[62 164 331 22]);
field1 = uidropdown(gui_fig,'Items',opt_list1,'Editable','on','Position',[64 143 329 22],'Value',opt_list1{1},'ValueChangedFcn',@ok_fun);
field2 = uidropdown(gui_fig,'Items',opt_list2,'Editable','on','Position',[64 81 329 22],'Value',opt_list1{3},'ValueChangedFcn',@cancel_fun);
uibutton(gui_fig,'push','Position',[64 32 100 22],'Text','Ok','ButtonPushedFcn',@ok_fun);
uibutton(gui_fig,'push','Position',[293 32 100 22],'Text','Cancel','ButtonPushedFcn',@cancel_fun);
function [str1 str2] = ok_fun(~,~)
disp('ahh')
str1 = field1.Value;
str2 = field2.Value;
closereq()
end
function cancel = cancel_fun(~,~)
disp('cancel')
cancel = 1;
closereq()
end
end
Categories
Find more on Graphics Objects 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!