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
回答 (1 件)
Cris LaPierre
2021 年 8 月 23 日
0 投票
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 件のコメント
Jennifer Krizman
2021 年 8 月 26 日
Cris LaPierre
2021 年 8 月 27 日
編集済み: Cris LaPierre
2021 年 8 月 27 日
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
カテゴリ
ヘルプ センター および File Exchange で App Building についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!