我做了一个快速测试函数来测试guidata,但每当我使用but3c时,我一直得到“输入参数不够”的错误。
是谁愿意帮忙解决这个问题?function test
clear
clc
figure();
but1 = uicontrol('units','normalized','position',[0.2,0.2,0.2,0.2],...
'callback',@but1cb);
but2 = uicontrol('units','normalized','position',[0.6,0.6,0.2,0.2],...
'callback',@but2cb);
but3 = uicontrol('units','normalized','position',[0.4,0.4,0.2,0.2],...
'callback',@but3cb,'string',' ');
function but1cb(hObject,~,handles)
a = 1;
handles.a = 1;
guidata(hObject, handles)
end
function but2cb(hObject,~,handles)
a = 2;
handles.a = 2;
guidata(hObject, handles)
end
function but3cb(hObject,~,handles)
a = handles.a
print(a);
end
end
答案 0 :(得分:2)
最简单的解决方法是首先从hObject获取guidata
function test
figure();
but1 = uicontrol('units','normalized','position',[0.2,0.2,0.2,0.2],...
'callback',@but1cb);
but2 = uicontrol('units','normalized','position',[0.6,0.6,0.2,0.2],...
'callback',@but2cb);
but3 = uicontrol('units','normalized','position',[0.4,0.4,0.2,0.2],...
'callback',@but3cb,'string',' ');
function but1cb(hObject,~)
a = 1;
handles = guidata(hObject);
handles.a = 1;
guidata(hObject, handles)
end
function but2cb(hObject,~)
handles = guidata(hObject);
a = 2;
handles.a = 2;
guidata(hObject, handles)
end
function but3cb(hObject,~)
handles = guidata(hObject);
a = handles.a
print(a);
end
end