How to only show image when button is pushed?

Hello all, I am trying to display several images in AppDesigner, depending on what button is pushed (both buttons should be able to be pushed at the same time).
I have two state button 'app.Button' and 'app.Button2'. I also have an Image 'app.Image'.
When 'Button' is pushed, I want 'pepper.png' to appear, and when 'Button2' is pushed, I want 'cucumber.png' to appear. For those purposes, I have created the following callbacks:
function ButtonValueChanged(app, event)
value = app.Button.Value;
app.Image.ImageSource = 'pepper.png';
end
function Button2ValueChanged(app, event)
value = app.Button2.Value;
app.Image.ImageSource = 'cucumber.png';
end
So far, this code works.
Now, I want the image 'pepper&cucumber.png' to appear if both Buttons are pushed.
Additionnally, if I deselect 'Button', I want 'pepper.png' to disappear (which is currently not the case, it just stays).
Thanks in advance for all your advice!

3 comentarios

Rik
Rik el 28 de Jul. de 2021
You have two functions that are dependent on eachother. You explain in words that button 1 must do this:
  • if it was not pressed and button 2 was not, show pepper.png
  • if it was pressed and button 2 was not, hide the image
  • if it was not pressed and button 2 was , show pepper&cucumber.png
  • if it was pressed and button 2 was , show cucumber.png
Do you see this behavior in your code?
DGM
DGM el 28 de Jul. de 2021
Editada: DGM el 28 de Jul. de 2021
Your callbacks are executed every time the button state changes
Each CBF records the state of the button, but never uses it (in this scope)
instead, it draws the selected picture regardless of button state.
Consider that what gets drawn needs to be a function of both button states. How you handle that is up to you.
EDIT:
I don't use AppDesigner, but for something simple like this, I'd normally just make one CBF that's used by both buttons. Check the states of both buttons and then use some conditional control structure to do whatever needs to be done to display each particular image or to clear the axes.
elevonm
elevonm el 28 de Jul. de 2021
Editada: Rik el 28 de Jul. de 2021
Thank you both for the help!

Iniciar sesión para comentar.

 Respuesta aceptada

elevonm
elevonm el 28 de Jul. de 2021
Editada: Rik el 28 de Jul. de 2021

0 votos

Ok this helped a lot, thank you! I defined a function in the methods that contains all the above if statements, and then I called this function in my callbacks.
Thank you so much!!

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Preguntada:

el 28 de Jul. de 2021

Editada:

Rik
el 28 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by