为什么这不起作用?正如您在下面的错误报告中所看到的那样,字段handles.check没有携带到回调函数中。
function example2_4
N=500;
M=300;
handles.fig=figure('Units','Pixels',...
'Position',[100 100 N M])
handles.axes=axes('Units','Pixels',...
'Position',[25 25 N-200 M-50]);
handles.check=uicontrol('style','checkbox',...
'string','Enable Axes Grid Lines',...
'Position',[N-150,M/2-25 150 50],...
'Callback',{@checkbox_callback,handles});
function checkbox_callback(gcf,event_data,handles)
handles
val=get(handles.check,'Value');
if val
grid on;
else
grid off;
end
错误报告:
handles =
fig: 2
axes: 331.0076
Reference to non-existent field 'check'.
Error in example2_4>checkbox_callback (line 19)
val=get(handles.check,'Value');
Error while evaluating uicontrol Callback
然而,这确实有效。这次handles.check被携带到回调函数中。
function example2_4
N=500;
M=300;
handles.fig=figure('Units','Pixels',...
'Position',[100 100 N M]);
handles.axes=axes('Units','Pixels',...
'Position',[25 25 N-200 M-50]);
handles.check=uicontrol('style','checkbox',...
'string','Enable Axes Grid Lines',...
'Position',[N-150,M/2-25 150 50]);
set(handles.check,'Callback',{@checkbox_callback,handles});
function checkbox_callback(gcf,event_data,handles)
handles
val=get(handles.check,'Value');
if val
grid on;
else
grid off;
end
处理报告:
handles =
fig: 3
axes: 488.0044
check: 489.0044
仅将的一个副本handles
传递到回调中。所以在
handles.check=uicontrol('style','checkbox',...
'string','Enable Axes Grid Lines',...
'Position',[N-150,M/2-25 150 50],...
'Callback',{@checkbox_callback,handles});
handles
,作为参数传递给checkbox_callback
,则仅设置了axes
和fig
字段,因为代码仅在创建check
字段的过程中。
而在
set(handles.check,'Callback',{@checkbox_callback,handles});
handles
(或复印件)有fig
,axes
和check
领域已经设置给前三个语句。
编辑
我总是使用GUIDE创建我的GUI,然后依靠该guidata
函数来获取和设置handles
结构中的字段。在您的示例中,您可能必须guidata
使用setappdata
andgetappdata
函数或使用and函数执行类似的操作。有关详细信息,请参见在回调之间共享数据。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句