MATLAB: Push button to change a variable

push button pushbutton gui

I guesss this is a very simple question, but I am spending more time searching for the answer, so I better ask here instead.
I made 3 pushbuttons, when I click on of them, a variable has to be changed, so like:
[Button1] when pressed: bp = sys
[Button2] when pressed: bp = mean
[Button3] when pressed: bp = dia
Thanks in advance

Best Answer

Hi Sven (nice name, btw), here's what I think you're looking for.
Try making a myFunction.m file with the contents below, then run it. Note that I assign bp in the parent function, outside any of the callback functions that the buttons activate. Because of this, these callback functions "share" bp and can change it and access it accordingly.
function myFunction()
bp = 'initial value'; % Assigned *outside* the callback functions so it is "shared" to them
figure
uicontrol( 'Position', [10 35 60 30],'String','Sys(R)','Callback',@(src,evnt)buttonCallback('sys') );
uicontrol( 'Position', [10 70 60 30],'String','Mean(B)','Callback',@(src,evnt)buttonCallback('mean') );
uicontrol( 'Position', [10 105 60 30],'String','Dia(G)','Callback',@(src,evnt)buttonCallback('dia') );
uicontrol( 'Position', [100 105 60 30],'String','Print Value','Callback',@(src,evnt)printMyVar );
function buttonCallback(newString)
bp = newString;
end
function printMyVar()
disp(bp)
end
end