1

我做了一个快速测试函数来测试 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
4

1 回答 1

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
于 2013-06-07T04:06:55.583 回答