MATLAB Answers

0

Closing figs but keeping variables for later use

Asked by Kostas Staios on 13 Jul 2018 at 17:54
Latest activity Edited by Stephen Cobeldick on 15 Jul 2018 at 19:55

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 Comment

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

Thanks,

Chris

Sign in to comment.

Tags

3 Answers

Answer by Dennis
on 13 Jul 2018 at 18:49

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 Comments

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.

Sign in to comment.


Answer by Kostas Staios on 14 Jul 2018 at 9:59

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 Comments

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)

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.

Sign in to comment.


Answer by Kostas Staios on 15 Jul 2018 at 12:07
Edited by Stephen Cobeldick on 15 Jul 2018 at 19:55

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.

  0 Comments

Sign in to comment.