|||
有时我们在程序中自定义一个函数,对handles数据进行修改,然后guidata保存,可是运行后程序提示:“Reference to non-existent field….”之类的错误,这是怎么回事呢?该错误以下例说明:
如图,建立一个文本框,在OpeningFcn函数中设定显示值为1.
function untitled1_OpeningFcn(hObject, eventdata, handles,varargin)
handles.output =hObject;
initialize(hObject,handles); % 设定文本框显示值handles.x的值为1
set(handles.edit1,handles.x); % 文本框显示为1
% Updatehandles structure
guidata(hObject,handles);
function initialize(fig_handle,handles)
handles.x=1;
guidata(fig_handle,handles);
运行后提示错误:
“Reference to non-existent field ‘x’”,提示说找不到定义的x.很明显initialize函数并没有把handles.x传递给全局变量handles. handles虽然是全局变量,但还没有全局到所有子函数都是可见的。像callback这些关于component的函数,handles是可见的。但是,用户自定义的函数,handles是不可见的,用户不能自定义子函数中修改handles的值。
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-5-29 13:10
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社