GUI app callback functions problem

5 Ansichten (letzte 30 Tage)
Askic V
Askic V am 24 Okt. 2022
Kommentiert: Jan am 24 Okt. 2022
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

Akzeptierte Antwort

Jan
Jan am 24 Okt. 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 Kommentare
Askic V
Askic V am 24 Okt. 2022
Thank you Jan. Do I also need to use delete(h.f); which was suggested in the earlier thread?
Jan
Jan am 24 Okt. 2022
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by