wait for GUI data to continue the script
Afficher commentaires plus anciens
Hi,
I have a script which call a function (a GUI). Inside the GUI, I want to collect responses (selected radiobuttons) to multiple question and then based on the answers, continue the main script. My idea was to put the answers in the user data of the figure (GUI) and then check that in the script. My problem is that when I run the main script, it does not wait for the data. I do not know how much time to wait for the user data (so I cannot use pause(t)). The last thing that came to my mind, was using a while after calling the GUI, so that when the current figure's userdata becomes non-empty the script goes on. However, it does not allow me to interact with the GUI!
Here's my code for the GUI:
function tesst()
set(0,'units','pixels')
% get screen size
screenrect = get(0,'screensize');
%open a figure
fg = figure('Position',screenrect, ...
'menubar','none','resize','off','NumberTitle','off');
S.ax = axes('unit', 'pix', 'position',screenrect);
question_test = imread('question_test.JPG');
image(question_test,'Parent',S.ax)
axis off
% define a button area
bg_pos = [590 15 40 195];
S.bg = uibuttongroup(fg,'unit','pix','pos',bg_pos,...
'backgroundcolor','w');
% size of the radio_buttons
rd_size = 15;
left_edge_rd = round((bg_pos(3) - rd_size)/2);
S.rd(1) = uicontrol(S.bg, 'style', 'rad', 'unit', 'pix', 'position',...
[left_edge_rd 170 rd_size rd_size],'backgroundcolor', 'w');
S.rd(2) = uicontrol(S.bg, 'style', 'rad', 'unit', 'pix', 'position',...
[left_edge_rd 130 rd_size rd_size],'backgroundcolor', 'w');
S.rd(3) = uicontrol(S.bg, 'style', 'rad', 'unit', 'pix', 'position',...
[left_edge_rd 90 rd_size rd_size],'backgroundcolor', 'w');
S.rd(4) = uicontrol(S.bg, 'style', 'rad', 'unit', 'pix', 'position',...
[left_edge_rd 50 rd_size rd_size],'backgroundcolor', 'w');
S.rd(5) = uicontrol(S.bg, 'style', 'rad', 'unit', 'pix', 'position',...
[left_edge_rd 10 rd_size rd_size],'backgroundcolor', 'w');
set(S.bg,'SelectedObject',[]);
next = imread('next.jpg');
uicontrol('style', 'pushbutton', 'pos' , [50 55 75 50],...
'fontsize', 18, 'cdata', next, 'callback' , {@next_callback});
count_que = 1;
max_questions = 2; %for now
question_ans = [2;3];
selected_ans = zeros(size(question_ans));
function next_callback(~,~) %go to the next question
for count_ans = 1:5
if get(S.bg,'SelectedObject') == S.rd(count_ans)
selected_ans(count_que) = count_ans;
end
end
fprintf("Question number: %d Participant's answer:%d\n",count_que,selected_ans(count_que))
if count_que >= max_questions
%if passed return true, else return false
fg.UserData = selected_ans(1);
waitforbuttonpress;
else
count_que = count_que + 1; %go to next question
question_test = imread(['question_test' int2str(count_que) '.jpg']);
image(question_test)
axis off
end
end
end
When the question reaches its end (by updating count_que after each next_callback press), I update the userdata and it becomes non-empty.
And here's a sample code for the main script:
tesst()
pause(1) %I used this pause to allow the GUI to get opened!
s = 2;
while isempty(get(gcf,'UserData')) == true
end
if s == get(gcf,'UserData')
disp('hoora')
else
disp('Damn it!')
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!