Code for action when ui toggle button is pressed

21 visualizaciones (últimos 30 días)
Gurra
Gurra el 8 de Mzo. de 2021
Comentada: Jorg Woehl el 10 de Mzo. de 2021
I have a uibuttongroup in a ui figure. It is only one button in the group. I am trying to use the SelectionChengedFcn to activate some code once the button is pressed by the pointer. In this example, I would like the string "test" to be displayed. But nothing happens. What do I need to add? As I understand it the value of my button is set to 1 from the beginning since there is only 1 button. Possibly that is a part of the problem. Also I am a bit uncertain about the input arguments to the bselection function. Here is my code after the declarations of the parameters for the positions:
fig = uifigure('Name',''test')
bg = uibuttongroup(fig,'Position',[x_bg 0.1*h w_bg h_bg],'SelectionChangedFcn',@bselection);
tb1 = uitogglebutton(bg,'Position',[x_tb1 y_tb1 w_tb1 h_tb1],'Text','Calc');
function bselection(source,event)
disp('test')
end
Thanks.

Respuesta aceptada

Jorg Woehl
Jorg Woehl el 8 de Mzo. de 2021
Editada: Jorg Woehl el 8 de Mzo. de 2021
Yes, that's indeed the problem - the selected button never changes because you only have one button in the button group, so the callback function is never triggered. If you add another button to your toggle button group, your code will work fine.
If you are only dealing with a single button, why don't you just use a state button?
As for the input arguments source and event to the callback function, you don't need to worry about them. What is important for you is only the value of selectedButton = app.ButtonGroup.SelectedObject in the case of the toggle button group, or value = app.Button.Value in the case of the state button, since your callback action will (normally) depend on that value.
  2 comentarios
Gurra
Gurra el 8 de Mzo. de 2021
Thanks for your answer, I have now changed to a state button. I really appreciate the advice. Now the string "test" is displayed once I push the button.
One last question, how do I write the function if I like "test" to be variable of some kind which I can use for operations?
Eg. test=3, and I like to perform say x=test*2 once I push the button. I tried to add "test" to the function inputs, but I get an error message.
Here is the code:
test=3;
bu = uibutton(fig,'Position',[x_bu y_bu w_bu h_bu],'Text','Calc','ButtonPushedFcn',@bselection);
function bselection(source, event,test)
x=2*test;
end
How do I send "test" to the function?
Thanks again.
Jorg Woehl
Jorg Woehl el 8 de Mzo. de 2021
Editada: Jorg Woehl el 8 de Mzo. de 2021
The function definitions in AppDesigner apps shouldn't be changed by the user, and there is no need to do that. You can operate on variables inside callback functions in two ways:
  1. Define the variable test inside the callback function and use it there. This, however, means that test is reset to the same initial value every time the callback is executed.
  2. Define a variable test outside the function but inside the app, which is done using public or private properties; see Share Data Within App Designer Apps for how to do this. You can then access the variable inside your callback function by calling it as app.test.
Happy coding!

Iniciar sesión para comentar.

Más respuestas (1)

Gurra
Gurra el 9 de Mzo. de 2021
Thanks Jorg Woehl for your input. I managed to solve it like this (leaving out code for uifigure and position varibales):
%%
test =3;
bu = uibutton(fig,'Position',[x_bu y_bu w_bu h_bu],'Text','Calc','ButtonPushedFcn',@(bu, event) bselection(bu,test));
function bselection(bu,test) %%callback
x=2*test;
disp(x)
end
%%Like this ui-objects can also be sent to the callback and be updated upon push on button
  1 comentario
Jorg Woehl
Jorg Woehl el 10 de Mzo. de 2021
Hi Gurra,
Yes, this works fine because you are creating your GUI programmatically - that was something I missed when I answered your original question.
If you create your GUI using AppDesigner, which is really neat (and the way to go for more complex user interfaces), you can manage variables in the way that I have described.

Iniciar sesión para comentar.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by