GUI app callback functions problem

Hello Matlab experts,
I'm modifying an existing GUI app. I need to add an additional functionality. There is a form with a button on it. When user clicks on this button, some code is executed. This code is written inside the pushbutton callback function.
I need to modify this in a way that when user clicks on the button, a new mini form (figure) is open with a couple of options to choose from. Only after user choose some of valid options (or all) the program can continue to work what is already programmed. The existing code includes input dialog and some other stuff.
I have added the form with options, but I noticed that the existing code was executed without waiting for user to choose options, so I added the condition to check whethere there are valid options choosen. However, this doesn't work becuase existing code is not executed. One solution I can think off is to add an infinite loop to check whether options are selected and if so, to break this loop and continue with program execution, but this also doesn't work.
But there could be a more efficient way. Do you have any suggestions? Probably I need to use uiwait, but I/m not sure how.
So this is what I tried:
function wButton_Callback(hObject, eventdata, handles)
% hObject handle to wButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%First read check box form
%***********************************
handles.checked = [];
handles.f = figure('Name','Options','NumberTitle','off', 'units','pixels','position',[600,600,250,150],...
'toolbar','none','menu','none');
set(handles.f, 'Resize', 'off')
% Create yes/no checkboxes
handles.c(1) = uicontrol('style','checkbox','units','pixels',...
'position',[10,120,70,15],'string','Option1');
handles.c(2) = uicontrol('style','checkbox','units','pixels',...
'position',[10,100,70,15],'string','Option2');
handles.c(3) = uicontrol('style','checkbox','units','pixels',...
'position',[10,80,70,15],'string','Option3');
handles.c(4) = uicontrol('style','checkbox','units','pixels',...
'position',[10,60,70,15],'string','Option4');
% % Create OK pushbutton
handles.p = uicontrol('style','pushbutton','units','pixels',...
'position',[10,10,70,20],'string','OK',...
'callback',@p_call);
% Pushbutton callback
function p_call(varargin)
vals = get(handles.c,'Value');
checked = find([vals{:}]);
handles.checked = checked;
close(handles.f);
end
disp(handles.checked); % Show chosen options (for debug)
%****************************************************
% Maybe to place a loop here or uiwait
if ~isempty(handles.checked)
% if no option is checked this code won't run
prompt = {'Additional data: '};
dlg_title = 'Enter additional data';
num_lines = 1;
defaultans = {'test'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
% Some exiting code to execute %
end
end

 Accepted Answer

Jan
Jan on 24 Oct 2022
I'm not sure if I understand, where in the code the problem occurs. Waiting for a figure to be closed is done by the waitfor function. An infinite loop is less efficient.
Another option is to let the CloseRequestFcn of the created figure call a new callback of the original figure.

4 Comments

Hello Jan,
perhaps I wasn't clear enough.
I needed to open another figure with check boxes and to let user choose options (1 or more). And then AFTER user clicks on OK button, the program executes depending on the options that were selected. The problem is the following:
when user click on a pushbutton and callback
wButton_Callback(hObject, eventdata, handles)
but the problem is that program doesn't waif for user to choose options, it executes lines below. For example figure with check boxes is open, and at the same time input dialog is open, but it should wait unitl user is done with checkbox options.
Basically I need to do something very similar to what was described here.
So I know that I need to use uiwat as suggested by TEST00, but not really sure how to do modify existing code.
Jan
Jan on 24 Oct 2022
Edited: Jan on 24 Oct 2022
handles.checked = [];
handles.f = figure('Name','Options','NumberTitle','off', 'units','pixels','position',[600,600,250,150],...
'toolbar','none','menu','none', 'Resize', 'off');
% Resize moved into the creation of the figure
% Create yes/no checkboxes
handles.c(1) = uicontrol('style','checkbox','units','pixels',...
'position',[10,120,70,15],'string','Option1');
... % The other buttons
% Create OK pushbutton
handles.p = uicontrol('style','pushbutton','units','pixels',...
'position',[10,10,70,20],'string','OK',...
'callback',@p_call);
% Pushbutton callback
function p_call(ButtonH, EventData)
vals = get(handles.c, 'Value');
handles.checked = find([vals{:}]); % Shorter
close(handles.f);
handles.f = []; % Safer
end
waitfor(handles.f); % <= wait after the figure was created
...
There are only some possible line, where the waitfor can be inserted: Before or after the p_call callback. You can simply try it to find out, that both locations are working.
Thank you Jan. Do I also need to use delete(h.f); which was suggested in the earlier thread?
Maybe. If the CloseRequestFcn contains some code to delete the figure, this will not work, because it is deleted afer close(handles.f) already. You can simply try it. If it works, it works. If you get an error message, change the code.
Do not hesitate to try, what the code does. You do not damage anything.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Asked:

on 24 Oct 2022

Commented:

Jan
on 24 Oct 2022

Community Treasure Hunt

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

Start Hunting!