Getting outputs from a uifigure when a button is pressed

26 views (last 30 days)
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

Voss
Voss on 13 Dec 2022
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);
% you can initialize output variables here, or define them
% ALL in each of ok_fun and cancel_fun:
str1 = '';
str2 = '';
cancel = 0;
% set gui_fig's CloseRequestFcn to @cancel_fun in case the user closes it
% the without clicking the Cancel button, but instead uses the X button (e.g., in
% the upper-right corner in Windows). In that case, you still want
% cancel_fun to have run and set cancel=1.
set(gui_fig,'CloseRequestFcn',@cancel_fun);
% don't return until gui_fig is deleted:
waitfor(gui_fig);
function ok_fun(~,~)
disp('ahh')
str1 = field1.Value;
str2 = field2.Value;
% cancel = 0; % make sure you define all output variables, either
% here and in cancel_fun, or set default values in
% the parent function above
closereq()
end
function cancel_fun(~,~)
disp('cancel')
% str = ''; % define all output variables
% str = '';
cancel = 1;
closereq()
end
end
  2 Comments
Toby Beisly
Toby Beisly on 14 Dec 2022
Awesome it works just as intended. Thanks for the comprehensive comments as well it makes lots of sense.

Sign in to comment.

More Answers (1)

VBBV
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
Toby Beisly
Toby Beisly on 13 Dec 2022
I've added the outputs to my functions but still get the same error and the upper level function still returns no outputs. Did i need to specify the outputs when i call the ok_fun in uibutton?
VBBV
VBBV on 14 Dec 2022
Edited: VBBV on 14 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

Sign in to comment.

Categories

Find more on Graphics Objects in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!