Closing figs but keeping variables for later use
3 次查看(过去 30 天)
显示 更早的评论
Hello, I will try to make myself as clear as I can. I am having two figs and when I press a button(the button next) in the first one, I want to close(I use the command close; the figure no1 and leave open only the figure np2. The problem is that I want to pass a variable(or more) from the first figure to the second and by using setappdata/getappdata the code does not work. Is there any way to do that, because otherwise I will have 7 open figures, in order to pass variables to the next one.
1 个评论
Christopher Wallace
2018-7-13
Could you post an example of the code you are using? Particularly the setappdata/getappdata portion.
Thanks,
Chris
回答(3 个)
Dennis
2018-7-13
Yes, you can use setappdata/getappdata.
Does not work is a rather weak describtion of the problem, without knowing what the problem is or seeing any code it is harder to provide an answer that does help you.
Minimalistic example of 2 figures passing a value from one to another and closing figure one (does only work if you push the button in figure 1 first):
handles.fig1=figure;
handles.fig2=figure;
handles.pb1=uicontrol('parent',handles.fig1,'style','pushbutton','callback',@fig1_callback);
handles.pb2=uicontrol('parent',handles.fig2,'style','pushbutton','callback',@fig2_callback);
setappdata(handles.pb1,'h',handles)
setappdata(handles.pb2,'h',handles)
function fig1_callback(hObj,~)
handles=getappdata(hObj,'h');
myvalue=randi(5);
setappdata(handles.pb2,'value',myvalue)
close(handles.fig1)
end
function fig2_callback(hObj,~)
myvalue=getappdata(hObj,'value');
disp(myvalue)
end
5 个评论
Dennis
2018-7-14
Wouldn't it be possible to pass 'signal' as additional function argument to fig2?
fig_no2(signal)
Walter Roberson
2018-7-14
Dennis,
Yes. Now that I have had a chance to check that in more detail, I find that if you pass in an argument that is not a character vector as the first argument, then the arguments will be passed as varargin to the OpenFcn, after hObject, event, and handles. So the OpenFcn could be programmed to accept the signal and record it.
It appears OpenFcn will be called even if the GUI already exists.
Kostas Staios
2018-7-14
2 个评论
Dennis
2018-7-14
You need to setappdata in your checkbox_single_Callback.
function checkbox_single_Callback(hObject, eventdata, handles)
% hObject handle to checkbox_single (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox_single
if get(handles.checkbox_single,'Value') == 1 && get(handles.checkbox_multiple,'Value') == 0
signal = 2;
else
signal = 3;
end
if get(handles.checkbox_single,'Value') == 1 && get(handles.checkbox_multiple,'Value') == 0
set(handles.checkbox_multiple,'Enable','off');
else
set(handles.checkbox_multiple,'Enable','on');
end
if get(handles.checkbox_single,'Value') == 0 && get(handles.checkbox_multiple,'Value') == 0
set(handles.pushbutton_next,'Enable','off');
elseif get(handles.checkbox_single,'Value') == 1 || get(handles.checkbox_multiple,'Value') == 1
set(handles.pushbutton_next,'Enable','on');
end
setappdata(gcf,'signal',signal)
Dennis
2018-7-14
编辑:Dennis
2018-7-14
As an alternative (you still need to setappdata in your pushbutton callback):
Your fig1 pushbutton callback:
function pushbutton_next_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_next (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fig1=gcf;
signal = getappdata(fig1, 'signal');
fig2 = zb2(signal);
close(fig1);
And fig2 OpeningFcn:
function zb2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to zb2 (see VARARGIN)
% Choose default command line output for zb2
handles.output = hObject;
guidata(hObject, handles);
signal=varargin{1} %give time for fig1 to do the setappdata
if signal == 2
set(handles.checkbox_multiple, 'enable', 'off');
set(handles.checkbox_single,'value',1)
set(handles.checkbox_single, 'enable', 'off');
elseif signal == 3
set(handles.checkbox_single, 'enable', 'off');
set(handles.checkbox_multiple,'value',1);
set(handles.checkbox_multiple, 'enable', 'off');
end
Thank you, Walter for clarification.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!