【问题标题】:Passing data from MATLAB GUI to another function将数据从 MATLAB GUI 传递到另一个函数
【发布时间】:2014-02-26 09:46:37
【问题描述】:

我有一个 matlab GUI (Compare2ImagesGUI),它在另一个 GUI (DistanceOrderGUI) 中被调用,并且应该根据与用户的某些交互返回一个变量。

这里是Compare2ImagesGUI 的调用方式:

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

result = Compare2ImagesGUI('DistanceOrderGUI', handles)

这是它打开时的作用:

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);

这是初始化函数:

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);

当用户完成交互时,他按下一个按钮,GUI 应该关闭并将值返回给它的调用函数:

% --- 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

我遵循了有关如何构造此代码的说明,找到了 herehere,但我收到了这个奇怪的错误,我真的不知道该怎么办:

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

请注意,第 127 行在 function figure1_CloseRequestFcn(hObject, handles) 函数中。有什么建议吗?

【问题讨论】:

  • 解决方案对您有用吗?
  • 是的,figure1_CloseRequestFcn 的版本应该已经解决了,所以我很想知道什么不起作用。

标签: matlab user-interface matlab-guide


【解决方案1】:

通常CloseRequestFcn 不是通过您创建的uicontrol 调用的,而是在以下情况下调用:

  • 您使用内置的图形控件(例如顶部的“X”框或图形菜单)关闭图形
  • close 被称为你的身影
  • 退出 MATLAB

发生的情况是pushbutton3_Callback它自己的句柄而不是hObject 中的图形的句柄传递给figure1_CloseRequestFcn。问题是'waitstatus' 属性只属于figure

解决方案是修改pushbutton3_Callback 以传递图形句柄,或修改pushbutton3_Callback 以仅使用图形句柄。例如:

% --- 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

注意:我在figure1_CloseRequestFcn 中添加了和eventdata 参数,您的代码中似乎缺少该参数。 (通常定义为@(hObject,eventdata)guitest('figure1_CloseRequestFcn',hObject,eventdata,guidata(hObject)))。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多