Effacer les filtres
Effacer les filtres

wait for GUI data to continue the script

26 vues (au cours des 30 derniers jours)
Zahra Arj
Zahra Arj le 20 Fév 2021
Commenté : Zahra Arj le 20 Fév 2021
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

Walter Roberson
Walter Roberson le 20 Fév 2021
If you use uiwait() and pass in a figure, then MATLAB will wait for the figure to be deleted.
If you use waitfor() and pass in (only) a figure, then MATLAB will wait for the figure to be deleted.
If you use waitfor() and pass in an object and a property name, the MATLAB will wait for the property of the object to change state.
You can name the UserData property as the property to be watched.
while isempty(get(gcf,'UserData')) == true
Caution: gcf changes if the user deletes a figure, or if the user clicks on a different figure. Including if they click on the figure as part of moving it aside visually for some reason that has nothing to do with the computation.
It is therefore safer to record the relevant figure handle first, and then refer to that figure handle, so that you do not accidentally switch attention to something else.
  1 commentaire
Zahra Arj
Zahra Arj le 20 Fév 2021
Many thanks Walter!
The problem was solved using waitfor(figurehandle, 'UserData')

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Specifying Target for Graphics Output dans Help Center et File Exchange

Tags

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by