すべてのネストされた関数は、親関数で定義された変数にアクセスできます。これは、すべてのコールバック関数でデータを共有するために使用できます。ここ
を説明するための例である:
function myGUI()
%# this variable is accessible in both callback functions
x = 0;
%# a simple GUI to increment/show the variable x
figure('Position',[300 300 350 150])
uicontrol('style','pushbutton', 'String','increment', ...
'Position',[50 50 100 30], 'Callback',@incrementCallback);
uicontrol('Style','pushbutton', 'String','get', ...
'Position',[200 50 100 30], 'Callback',@getCallback);
%# callback functions
function incrementCallback(src,evt)
x = x + 1;
end
function getCallback(src,evt)
msgbox(sprintf('x = %d',x))
end
end