GUIDE中的句柄变量未更新 [英] Handles variable in GUIDE is not updating

查看:149
本文介绍了GUIDE中的句柄变量未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用MATLAB GUIDE创建的GUI.我正在尝试从GUI返回值.以下是代码的相关部分(完整的代码可在此处找到):

I have a GUI created using MATLAB GUIDE. I am trying to return a value from the GUI. Here are the relevant parts of the code (complete code can be found here):

function varargout = test(varargin)

% --- Outputs from this function are returned to the command line.
function varargout = test_OutputFcn(hObject, eventdata, handles) 

    % Get default command line output from handles structure
    varargout{1} = handles.output;
    varargout{2} = handles.test;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)

    handles.test = 'ok';

    % Update handles structure
    guidata(hObject, handles);

执行GUI后,我立即收到以下错误消息:

And as soon as I execute the GUI, I get this error message:

我在MATLAB新闻组上发现了一个类似问题,但我没有找不到解决方案(我读了所有guidata的文档,如建议的那样.)

I found a similar question on the MATLAB newsgroup, but I didn't find a solution (and I read all the guidata's doc, like suggested).

我的问题是我在一个GUIDE函数的句柄"结构中记录了一条信息,而在另一函数中却找不到该信息.

My problem is that I recorded an information in the "handles" structure within one GUIDE's function and I can't retrieve this information in another function.

尝试以取消注释test_OpeningFcn函数中UIWAIT调用的注释,尝试等待用户关闭窗口.

I tried to un-comment the UIWAIT call in the test_OpeningFcn function in an attempt to wait for the user to close the window.

% --- Executes just before test is made visible.
function test_OpeningFcn(hObject, eventdata, handles, varargin)

    % Choose default command line output for test
    handles.output = hObject;

    % Update handles structure
    guidata(hObject, handles);

    % UIWAIT makes test wait for user response (see UIRESUME)
    uiwait(handles.figure1);

单击按钮后,然后尝试获取handle变量.但这也不起作用,并且失败并显示类似错误消息.

After I click in button, and then try to take the handle variable. But it doesn't work either and fails with a similar error message.

任何帮助都将受到欢迎. 感谢您的关注.

Any help will be welcome. Thanks for your attention.

推荐答案

问题是当您以以下方式调用GUI时:

The problem is that when you call the GUI as:

>> [a,b] = test()

此调用立即返回,因此在输出函数中尝试访问尚不存在的handles.test,从而导致错误.

this call returns immediately, so in the output function you try to access handles.test which does not exist just yet, causing the error.

有一个显示了如何从GUI返回值.以下是要从该页面复制的更改列表:

There is a screencast by Doug Hull showing how to return a value from a GUI. Here is the list of changes to make copied from that page:

%%% OpenignFCN
uncomment uiwait

%%%OutputFCN
varargout{1} = handles.output;
% The figure can be deleted now
delete(handles.figure1);


%%%CloseReqFCN
if isequal(get(hObject, 'waitstatus'), 'waiting')
    % The GUI is still in UIWAIT, us UIRESUME
    uiresume(hObject);
else
    % The GUI is no longer waiting, just close it
    delete(hObject);
end

这篇关于GUIDE中的句柄变量未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆