Matteo
Matteo

Reputation: 8142

Passing data from MATLAB GUI to another function

I have a matlab GUI (Compare2ImagesGUI) which gets called within another GUI (DistanceOrderGUI) and should return a variable based on some interaction with a user.

Here is a snippet of how the Compare2ImagesGUI gets called:

a=1;b=2;
handles.a = a;
handles.b = b;

result = Compare2ImagesGUI('DistanceOrderGUI', handles)

And here is what it does when it opens:

function Compare2ImagesGUI_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 Compare2ImagesGUI (see VARARGIN)

% Choose default command line output for Compare2ImagesGUI
%handles.output = hObject;
a = varargin{2}.a;
b = varargin{2}.b;
handles.a = a;
handles.b = b;
handles.ima = varargin{2}.ims{a};
handles.imb = varargin{2}.ims{b};
init(hObject,handles);

% UIWAIT makes Compare2ImagesGUI wait for user response (see UIRESUME)
uiwait(hObject);

this is the init function:

function init(hObject,handles)

imshow(handles.ima,'Parent',handles.axes1);
handles.current = handles.a;

% handles.ims=ims; handles.gt=gt; 
% handles.folderN=folderN; handles.name=dirnames{folderN};

% Update handles structure
guidata(hObject, handles);

When the user finishes interacting he presses a button and the GUI should close and return the value to its calling function:

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
figure1_CloseRequestFcn(hObject,handles);

% --- Outputs from this function are returned to the command line.
function varargout = Compare2ImagesGUI_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.current

delete(handles.Compare2ImagesGUI);


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if isequal(get(hObject,'waitstatus'),'waiting')
    uiresume(hObject);
else
    delete(hObject);
end

I followed instruction on how to structure this code found here and here, nonetheless I get this weird error and am really clueless on what to do about it:

Error using hg.uicontrol/get
The name 'waitstatus' is not an accessible property for an instance of
class 'uicontrol'.

Error in Compare2ImagesGUI>figure1_CloseRequestFcn (line 127)
if isequal(get(hObject,'waitstatus'),'waiting')

Error in Compare2ImagesGUI>pushbutton3_Callback (line 118)
figure1_CloseRequestFcn(hObject,handles);

Error in gui_mainfcn (line 96)
        feval(varargin{:});

Error in Compare2ImagesGUI (line 42)
    gui_mainfcn(gui_State, varargin{:});

Error in
@(hObject,eventdata)Compare2ImagesGUI('pushbutton3_Callback',hObject,eventdata,guidata(hObject))


Error using waitfor
Error while evaluating uicontrol Callback

Note that line 127 is in the function figure1_CloseRequestFcn(hObject, handles) function. Any suggestions?

Upvotes: 2

Views: 1833

Answers (1)

chappjc
chappjc

Reputation: 30589

Usually a CloseRequestFcn is not called via a uicontrol that you created, but rather when:

  • you close the figure using the built-in figure controls (e.g the "X" box at the top, or the figure menu)
  • close is called for your the figure
  • quit MATLAB

What happens is that pushbutton3_Callback passes its own handle rather the figure's handle in hObject to figure1_CloseRequestFcn. The problem is that the 'waitstatus' property only belongs to a figure.

The solution is to either modify pushbutton3_Callback to pass the figure handle, or modify pushbutton3_Callback to just use the figure handle. For example:

% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

hFig = ancestor(hObject,'Figure');
if isequal(get(hFig,'waitstatus'),'waiting')
    uiresume(hFig);
else
    delete(hFig);
end

NOTE: I added and eventdata argument to figure1_CloseRequestFcn, which seemed to be missing from your code. (Normally it is defined as @(hObject,eventdata)guitest('figure1_CloseRequestFcn',hObject,eventdata,guidata(hObject))).

Upvotes: 1

Related Questions