1

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

Whyaken
  • 71
  • 3
  • 8

2 Answers2

4

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
tim
  • 9,896
  • 20
  • 81
  • 137
0

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).

Jorge
  • 784
  • 5
  • 16
  • Good hint. Yeah I already wanted to post something about the `global` thing, which also could have been solved by using attributes of a handle-class, which surely would have been of a overhead in this case surely, but if the function is going to be extended, it might be helpful – tim Feb 01 '12 at 07:45