MATLAB: How to Change a Value of parameter in GUI and automatically change in script file.

gui button global callback matlab

i think the answer is easy but i'm new in MATLAB world.. i have two buttons if the user press any of them the Value of I1 will change.
this is my function:
function PU = testfun
I1=0;
B1=uicontrol('Style',...
'pushbutton',...
'String',' 1 ',...
'Position',[10 350 100 30],...
'HandleVisibility','on',...
'Callback',@PushB1);
B2=uicontrol('Style','pushbutton',...
'String',' 2 ',...
'Position',[10 150 100 30],...
'HandleVisibility','on',...
'Callback',@PushB2);
function PushB1(~,~)
I1=1;
end
function PushB2(~,~)
I1=2;
end
end
And here is the script :
GS=20;
PU = testfun
if I1 ==1
GS=GS-5;
end
if I1 ==2
GS=GS-10;
end
I DID TRY GLOBAL BUT IT DID NOT WORK. thank you.

Best Answer

function PU = testfun
H = figure;
uicontrol('Style',...
'pushbutton',...
'String',' 1 ',...
'Position',[10 350 100 30],...
'HandleVisibility','on',...
'Callback', {@PushB, 1});
uicontrol('Style','pushbutton',...
'String',' 2 ',...
'Position',[10 150 100 30],...
'HandleVisibility','on',...
'Callback', {@PushB, 2});
set(H, 'UserData', 0);
uiwait(H); % Let Matlab wait until UIRESUME is called
PU = get(H, 'UserData');
end
function PushB(ButtonH, ~, Num) % Same callback for both buttons
H = ancestor(ButtonH, 'figure');
set(H, 'UserData', Num);
uiresume(H);
end