GUI app callback functions problem

조회 수: 6 (최근 30일)
Askic V
Askic V 2022년 10월 24일
댓글: Jan 2022년 10월 24일
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

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by