push button to change a variable

26 visualizzazioni (ultimi 30 giorni)
Sven Schoeberichts
Sven Schoeberichts il 31 Gen 2012
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 Commento
Sven Schoeberichts
Sven Schoeberichts il 31 Gen 2012
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?

Accedi per commentare.

Risposta accettata

Sven
Sven il 31 Gen 2012
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 Commento
Sven Schoeberichts
Sven Schoeberichts il 3 Feb 2012
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

Accedi per commentare.

Più risposte (1)

bimal raj
bimal raj il 31 Gen 2012
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 Commento
Sven Schoeberichts
Sven Schoeberichts il 31 Gen 2012
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

Accedi per commentare.

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by