push button to change a variable

조회 수: 25 (최근 30일)
Sven Schoeberichts
Sven Schoeberichts 2012년 1월 31일
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
Sven Schoeberichts 2012년 2월 3일
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