GUI Subfunction not passing variable to parent function

I created a GUI (not using GUIDE) for a listening task that I am running. For the task, the listener hears a sentence and is asked to repeat the sentence. The tester uses a GUI to select which words in the sentence the listener has correctly recalled. Below is the relevant bit of code. For this GUI, the tester would select the check boxes next to the correctly recalled words. Thenn when the tester hits the 'next' button, the values for each of those check boxes would get stored in a variable called 'cbx_vals'. While the code within the pushbutton1_callback executes correctly, with cbx_vals being populated by the values for each checkbox, these values are not being passed back to the parent function. I've tried making 'cbx_vals' a global variable and creating it as an empty variable outside the pushbutton1_callback function but neither approach as worked. Thanks in advance for your help!
cbx_vals=[];
words={'I';'believe';'that';'for';'his';'escape';'he';'took';'advantage';'of';'the';'migration';'of';'a';'flock';'of';'wild';'birds'};
from_bottom = 240;
fig = figure('Visible','on','Name','ALLSSTAR Scoring','Position',[360,100,600,300 + from_bottom]);
set(fig,'color',[0.95 0.95 0.95]);
title_text = uicontrol('Style','text','String','ALLSSTAR Scoring','FontSize',20,'Position',[150 (260 + from_bottom) 300 40]);
set(title_text,'BackgroundColor',[0.95 0.95 0.95]);
x=230;
for w=1:length(words)
selected_g1 = uicontrol('Style','text','String',words{w},'Position',[200 (x + from_bottom) 160 20]);
cbx(w) = uicontrol('Style', 'checkbox', 'Value', 0, 'Position',[150 (x + from_bottom) 50 20]);
set(selected_g1,'BackgroundColor',[0.95 0.95 0.95]);
set(selected_g1, 'fontsize', 12, 'horizontalalign', 'left');
% set(cbx(w),'Callback',@getcbx);
x=x-25;
end
pushbuttonAll = uicontrol('Style','pushbutton','String','All','FontSize',16,'Position',[400 (130+from_bottom) 80 35]);
set(pushbuttonAll,'Callback',{@pushbuttonAll_Callback, cbx});
pushbuttonNone = uicontrol('Style','pushbutton','String','None','FontSize',16,'Position',[400 (90+from_bottom) 80 35]);
set(pushbuttonNone,'Callback',{@pushbuttonNone_Callback, cbx});
pushbutton1 = uicontrol('Style','pushbutton','String','Next','FontSize',16,'Position',[400 (from_bottom) 100 50]);
set(pushbutton1,'Callback',{@pushbutton1_Callback, cbx, fig});
function pushbuttonAll_Callback(source,event,cbx)
for c=1:length(cbx)
set(cbx(c), 'Value', 1);
end
end
function pushbuttonNone_Callback(source,event,cbx)
for c=1:length(cbx)
set(cbx(c), 'Value', 0);
end
end
function pushbutton1_Callback(source,event,cbx,fig)
for c=1:length(cbx)
cbx_vals(c) = get(cbx(c), 'Value');
end
close(fig)
end

Answers (1)

Typically this would be done in figure-based app by adding a structure to the figure, and adding your variables to that structure. In GUIDE, it was the handles structure.
The linked page provides several options to use. Explore that and follow up with any questions you have.

2 Comments

Thank you. I am able to get it working for a single checkbox, but I will need it to work for a dynamic number of checkboxes. Although the example contains a sentence with a set number of words, the number of words on each trial will vary. So we need to be able to grab the value for each check box but I don't see how to do that with the options provided on the linked page. Below is the code to pull the values for just the first word of the sentence, but we will need to have a loop for w=1:length(words) when creating the 'selected_g1' and 'cbx' variables, which includes setting the 'getcbx' callback function. Any help on getting this working for all words in the sentence would be appreciated.
function answerGUI_forhelponecheckbox()
words={'I';'believe';'that';'for';'his';'escape';'he';'took';'advantage';'of';'the';'migration';'of';'a';'flock';'of';'wild';'birds'};
global data
fig = figure('Visible','on','Name','ALLSSTAR Scoring','Position',[360,100,600,530]);
set(fig,'color',[0.95 0.95 0.95]);
title_text = uicontrol('Style','text','String','ALLSSTAR Scoring','FontSize',20,'Position',[150 490 300 40]);
set(title_text,'BackgroundColor',[0.95 0.95 0.95]);
guidata(fig,struct('val',0));
selected_g1 = uicontrol('Parent', fig,'Style','text','String',words{1},'Position',[200 470 160 20]);
cbx = uicontrol('Parent', fig,'Style', 'checkbox', 'Value', 0, 'Position',[150 470 50 20]);
set(selected_g1,'BackgroundColor',[0.95 0.95 0.95]);
set(selected_g1, 'fontsize', 12, 'horizontalalign', 'left');
set(cbx,'Callback',@getcbx);
pushbutton1 = uicontrol('Parent', fig,'Style','pushbutton','String','Next','FontSize',16,'Position',[400 240 100 50]);
set(pushbutton1,'Callback',{@pushbutton1_Callback});
function getcbx(hObject, eventdata)
data = guidata(hObject);
data.val=hObject.Value;
guidata(hObject, data);
end
function pushbutton1_Callback(hObject,eventdata)
data = guidata(hObject);
display(data.val);
close(fig)
end
end
Why do you need a callback for each checkbox? I think you can get by with a single callback in your pushbutton. When clicked, that callback can query all the checkboxes and then use those values to determine what words were selected.
You seem to have taken the nested function approach, which means you do not have to use guidata.
Try this code. Note I am using strings instead of cells as it makes it a little easier to work with text. I've had to do some math with the positioning to be able to place everything dynamically.
function answerGUI_forhelponecheckbox()
words=["I";"believe";"that";"for";"his";"escape";"he";"took";"advantage";"of";"the";"migration";"of";"a";"flock";"of";"wild";"birds"];
fig = figure('Visible','on','Name','ALLSSTAR Scoring','Position',[360,100,600,max(300,50+(length(words)+1)*30)]);
set(fig,'color',[0.95 0.95 0.95]);
title_text = uicontrol('Style','text','String','ALLSSTAR Scoring','FontSize',20,'Position',[100 (length(words)+1)*30 300 40]);
set(title_text,'BackgroundColor',[0.95 0.95 0.95]);
for c=1:length(words)
selected_g1 = uicontrol('Parent', fig,'Style','text','String',words(c),'Position',[200 (length(words)-c+1)*30 160 20]);
cbx(c) = uicontrol('Parent', fig,'Style', 'checkbox', 'Value', 0, 'Position',[150 (length(words)-c+1)*30 50 20]);
set(selected_g1,'BackgroundColor',[0.95 0.95 0.95]);
set(selected_g1, 'fontsize', 12, 'horizontalalign', 'left');
end
pushbutton1 = uicontrol('Parent', fig,'Style','pushbutton',...
'String','Next','FontSize',16,'Position',[400 240 100 50],...
'Callback',@pushbutton1_Callback);
function pushbutton1_Callback(hObject,eventdata)
for b=1:length(cbx)
val(b) = cbx(b).Value;
end
words(val==1)
close(fig)
end
end

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 23 Aug 2021

Edited:

on 27 Aug 2021

Community Treasure Hunt

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

Start Hunting!