Whyaken
Whyaken

Reputation: 71

pushbutton to change variable

I guess this is a very simple question, but I am spending more time searching for the answer, than I would if I'd ask here

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

This is what I have so far, I copied the code from a button that resumes a script. What do I need to adjust to fit my need?

kiessys = uicontrol( 'Position', [10 35 60 30],'String','Sys(R)','Callback','uiresume( gcbf )' );
kiesmean = uicontrol( 'Position', [10 70 60 30],'String','Mean(B)','Callback','uiresume( gcbf )' );
kiesdia = uicontrol( 'Position', [10 105 60 30],'String','Dia(G)','Callback','uiresume( gcbf )' );

Thanks in advance

Upvotes: 1

Views: 3647

Answers (2)

Jorge
Jorge

Reputation: 784

Alexandrew's answer is good, however you can do it without using the "fun" function. Just type in the "callback" string the commands, i.e.

kiessys = uicontrol( 'Position', [10 35 60 30],'String','Sys(R)','Callback', 'bp = sys;');
kiesmean = uicontrol( 'Position', [10 70 60 30],'String','Mean(B)','Callback','bp = mean;');
kiesdia = uicontrol( 'Position', [10 105 60 30],'String','Dia(G)','Callback', 'bp = dia;');

The commands will run in the "base" workspace and the variables will be visible to any script. This way you don't have to declare them as global, which is generally not a good practice.

A note on creating GUIs in Matlab. It is a good practice (best actually) to use GUIDE to create the gui than using commands, as it simplifies things considerably and is a lot faster in development (just consider that you have to create 10 buttons, 2 axes etc using commands... positioning them alone is a nightmare).

Upvotes: 0

tim
tim

Reputation: 10186

there you go:

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

Upvotes: 4

Related Questions