Closing figs but keeping variables for later use

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 comentario

Could you post an example of the code you are using? Particularly the setappdata/getappdata portion.
Thanks,
Chris

Iniciar sesión para comentar.

Respuestas (3)

Dennis
Dennis el 13 de Jul. de 2018
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 comentarios

Kostas Staios's "Answer" moved here:
Ok, so I am trying to pass the variable signal to fig no2 with the callbacks in tww checkboxs.
hIdentGui = getappdata(0,'hIdentGui');%get ta data
if get(handles.checkbox_single,'Value') == 1 && get(handles.checkbox_multiple,'Value') == 0
signal = 0;
else
signal = 1;
end
setappdata(hIdentGui,'signal',signal);
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
And for hIdentGui, in the OpeningFcn I use: setappdata(0,'hIdentGui',gcf);
In fig no1, I have the next_button, which goes to fig no2 and the code is simple(in fig no1):
fig_no2;
close;
Now, in fig no2, in the OpeningFcn, I have this code:
hIdentGui = getappdata(0,'hIdentGui');%get ta data
signal = getappdata(hIdentGui,'signal');
The code is working without the close function in fig no1. I hope I make myself clear now. What seems to be the problem?
"And for hIdentGui, in the OpeningFcn I use: setappdata(0,'hIdentGui',gcf);"
You are doing that in which OpeningFcn ?
If you are doing that in the OpeningFcn for the first GUI, then you are storing the handle to the first figure in the appdata . Then you close that figure. Then you retrieve the handle to that now-deleted figure and expect to be able to retrieve app data from it. appdata goes away when the graphics object it is stored against is deleted.
If fig_no2 is written in GUIDE, then if you were to do
fig1 = gcf;
signal = getappdata(fig1, 'signal');
fig2 = fig_no2;
setappdata(fig2, 'signal', signal);
close(fig1);
then you would have transferred the data to the figure of fig_no2, which could then have
pause(1); %give time for fig1 to do the setappdata
fig2 = gcf;
signal = getappdata(fig2, 'signal');
This is a bit risky because there could be an arbitrarily long time between when the first figure invokes fig_no2 to launch the GUI and the time it gets around to doing the setappdata() . There are more robust methods.
Wouldn't it be possible to pass 'signal' as additional function argument to fig2?
fig_no2(signal)
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.

Iniciar sesión para comentar.

Kostas Staios
Kostas Staios el 14 de Jul. de 2018

0 votos

Thanks for the answers, but my code doesnt seem to work when I put what Walter said. I made a simplier code so you can help me with what to add so my code to work properly. So, I have two figs, zb1 and zb2 and will attach both m and fig files, cause I think with my small experience I cant make myself clear with small part of the code.
So, when I put breakpoints, the variable signal is an empty matrix in zb2. Why is that and what can I change?

2 comentarios

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
Dennis el 14 de Jul. de 2018
Editada: Dennis el 14 de Jul. de 2018
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.

Iniciar sesión para comentar.

Kostas Staios
Kostas Staios el 15 de Jul. de 2018
Editada: Stephen23 el 15 de Jul. de 2018

0 votos

Thank you very much for your help, Dennis and Walter, my code is working nicely and smoothly now. :) If there is any other problem, I will ask here if it is similar.

Categorías

Más información sobre Line Plots en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 13 de Jul. de 2018

Editada:

el 15 de Jul. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by