Domanda

I have a MATLAB GUI which I have created using GUI Layout Toolbox. This GUI has a primary figure. The primary figure has a push button that calls a secondary figure. There is a function called through primary figure, which access some variables from secondary figure. At The beginning of the code or till the point the secondary figure is opened, everything works fine. Once the secondary is opened, if I keep it open, the function call works fine, but if i close the secondary figure, the function call stops working.

Below is a snippet of how I have defined my variables and function calls:

S.Fig1 = figure();
S.var1 = uicontrol('parent',S.Fig1,...
                      'style','edit');
S.Fig2 = figure();
S.var2 = uicontrol('parent',S.Fig2,...
                      'style','edit');

S.var1 is associated with function call var1_call() and inside that function I am checking value for S.var2. If the secondary figure is open, value is provided correctly, else the statement will show an error saying "invalid handle object"

Please let me know if I cannot define the two figures as I have and if I can then how can I check if the fig2 is closed after opening it once.

Thanks

Adding the below example code: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Test script to check calling of a new figure from main figure %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [] = TestCallingNewWindow()

SCR = get(0,'Screensize');  % Get screensize.

% Open Figure
S.fh = figure('numbertitle','off',...
              'menubar','none',...
              'units','pixels',...
              'position',[SCR(3)/2-800 ,SCR(4)/2-100 , 500, 200],...
              'name','TestCallingWindow',...
              'resize','off');

% Create PushButtons
S.pb1 = uicontrol('style','pushbutton',...
                  'units','pixels',...
                  'position',[20 120 200 30],...
                  'string','Open New Window',...
                  'fontsize',12);

for i=1:6
    S.Select(i) = uicontrol('parent',S.fh,...
                      'style','checkbox',...
                      'units','pixels',...
                      'position',[250 (165-((i-1)*30)) 30 20],...
                      'string',sprintf('%d',i),...
                      'enable','on',...
                      'fontsize',10);
    S.Type(i) = uicontrol('parent',S.fh,...
                      'style','text',...
                      'units','pixels',...
                      'position',[300 (165-((i-1)*30)) 60 20],...
                      'string','Data',...
                      'enable','on',...
                      'fontsize',10);
    S.TypeVal(i) = uicontrol('parent',S.fh,...
                      'style','edit',...
                      'units','pixels',...
                      'position',[365 (165-((i-1)*30)) 80 20],...
                      'string','0',...
                      'enable','on',...
                      'fontsize',10);
end

% Create the Pop-up Figure
S.popfh = figure('numbertitle','off',...
              'menubar','none',...
              'units','pixels',...
              'position',[SCR(3)/2-200 ,SCR(4)/2-100 , 300, 200],...
              'name','Pop-Up Window',...
              'resize','off',...
              'visible','off');

for i=1:6
    S.popSelect(i) = uicontrol('parent',S.popfh,...
                      'style','checkbox',...
                      'units','pixels',...
                      'position',[50 (165-((i-1)*30)) 30 20],...
                      'string',sprintf('%d',i),...
                      'enable','on',...
                      'fontsize',10);
    S.popType(i) = uicontrol('parent',S.popfh,...
                      'style','text',...
                      'units','pixels',...
                      'position',[100 (165-((i-1)*30)) 60 20],...
                      'string','Data',...
                      'enable','on',...
                      'fontsize',10);
    S.popTypeVal(i) = uicontrol('parent',S.popfh,...
                      'style','edit',...
                      'units','pixels',...
                      'position',[165 (165-((i-1)*30)) 80 20],...
                      'string','0',...
                      'enable','on',...
                      'fontsize',10);
end

% Set callback functions
set(S.Select(:),'callback',{@main_call,S})
set(S.TypeVal(:),'callback',{@main_call,S})
set(S.pb1,'callback',{@pb1_call,S}) 
set(S.popSelect(:),'callback',{@pb1_call,S})
set(S.popTypeVal(:),'callback',{@pb1_call,S})

% Function Definitions
function [] = main_call(varargin)
    [h,S] = varargin{[1,3]};  % Get calling handle and structure.
    popWin = findobj('type','figure','name','Pop-Up Window');
    for idx = 1:6
        if(~isempty(popWin))
            popenable = get(S.popSelect(idx),'Value');
        else
            popenable = 0;
        end
        if(popenable == 0)
            enable = get(S.Select(idx),'Value');
            if(enable == 1)
                data = str2double(get(S.TypeVal(idx),'String'));
                if(~isempty(popWin))
                    set(S.popTypeVal(idx),'string',data);
                end
            end
        else
            data = str2double(get(S.popTypeVal(idx),'String'));
        end
    end
end

% po-up window
function [] = pb1_call(varargin)
    [h,S] = varargin{[1,3]};  % Get calling handle and structure.

    set(S.popfh,{'visible'},{'on'});
    for idx = 1:6
        popenable = get(S.popSelect(idx),'Value');
        if(popenable == 0)
            enable = get(S.Select(idx),'Value');
            if(enable == 1)
                data = str2double(get(S.TypeVal(idx),'String'));
                set(S.popTypeVal(idx),'string',data);
            end
        else    % if popenable is 1
            data = str2double(get(S.popTypeVal(idx),'String'));
        end
    end
end

end
È stato utile?

Soluzione

How about naming your figures:

S.Fig1 = figure('name','figure1');

S.Fig2 = figure('name','figure2');

Then you can find them with findobj:

findobj('type','figure','name','figure2');

This will return the graphics handle if it is open, or be empty if it is closed. So this call will check if the figure exists:

~isempty(findobj('type','figure','name','figure2'))

sources:

How to check if a figure is opened and how to close it?

http://www.mathworks.com/help/matlab/ref/findobj.html

http://www.mathworks.com/help/matlab/ref/isempty.html

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% So I'm guessing you're getting the error on this line: set(S.popfh,{'visible'},{'on'}); in pb1_call if the figure is closed as described above. So instead of this you should have:

if ~isempty(findobj('type','figure','name','Pop-Up Window'))
    set(S.popfh,{'visible'},{'on'});
else
    S.popfh=figure('name','Pop-Up Window','Visible','on');
end

This is more robust and makes sure the figure is open before trying to change it's properties.

Altri suggerimenti

I'm not sure if I understand your problem. I think you are going to pass some variables between functions. If so, you must use "guidata" function. for example: you have read some data (lets name it DATA) in some call back function (name it MyFunc1) and want to use it in another one. If so, you must add these two line of code at the end of the exporting function:

function MyFunc1_Callback(hObject, eventdata, handles)
%
% BODY OF YOUR FUNCTION WHICH CREATES "DATA" VARIABLE
%


% attach DATA to handles structure
handles.DATA = DATA;
% Update handles structure
guidata(hObject,handles);

Then in the other function you can use the DATA stored in "handles" structure:

function MyFunc2_Callback(hObject, eventdata, handles)
data = handles.DATA;
...

I found the answer to my question. I had to define the CloseRequestFcn of the two figures separately and with that I was able to control all my requirements

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top