push button to change a variable

38 次查看(过去 30 天)
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
  1 个评论
Sven Schoeberichts
Sven Schoeberichts 2012-1-31
kiessys = uicontrol( 'Position', [10 35 60 30],'String','Sys(R)','Callback','????' );
kiesmean = uicontrol( 'Position', [10 70 60 30],'String','Mean(B)','Callback','????' );
kiesdia = uicontrol( 'Position', [10 105 60 30],'String','Dia(G)','Callback','????' );
This is what I have btw, what callback should I use, if any?

请先登录,再进行评论。

采纳的回答

Sven
Sven 2012-1-31
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
  1 个评论
Sven Schoeberichts
Thanks Sven, nice name indeed :D
I got a quick answer on stackoverflow which did what I needed. It seems like the code is very similar to yours, thanks anyway!
global bp;
figure
kiessys = uicontrol( 'Position', [10 35 60 30],'String','Sys(R)','Callback', {@fun, 'sys'});
kiesmean = uicontrol( 'Position', [10 70 60 30],'String','Mean(B)','Callback', {@fun, 'mean'});
kiesdia = uicontrol( 'Position', [10 105 60 30],'String','Dia(G)','Callback', {@fun, 'dia'});
kiesdia = uicontrol( 'Position', [10 140 200 30],'String','Output current value','Callback', 'disp(bp)');
and store the callback-function fun to fun.m:
function fun(~, ~, value)
global bp;
bp = value;
end

请先登录,再进行评论。

更多回答(1 个)

bimal raj
bimal raj 2012-1-31
sorry i didnt see your comment
In this code kiessys = uicontrol( 'Position', [10 35 60 30],'String','Sys®','Callback','????' );
you are not setting the style property..
you can try this
h1=uicontrol('Style','text','position',[a b c d],'tag','text1','String','Sven');
push buttons may be created like this
h2= uicontrol('Style','pushbutton','String','Sys(R)','callback','pb1_callback','position',[a b c d]);
in the call back function 'pb1_callbck' add this code
set(findobj('tag','text1'),'String','bp=mean');
similarly you can create other push buttons and write the call back functions.
uicontrol handles also can be used for changing properties. matlab help contains many example programs
  1 个评论
Sven Schoeberichts
Sven Schoeberichts 2012-1-31
Does this change the text on the button? Because that is not what I need...
I have 3 signals, and the user has to choose one of the three, by clicking the corresponding button.
By clicking the button, the variable 'bp' should be set to either 'sys', 'dia', or 'mean', depending on the button that is pressed

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by