How do I create a color changing push button?

2 views (last 30 days)
Hey there,
Im designing a GUI and trying to create a pushbutton that changes its color after every time being clicked. After clicked the first time it should change to red, then green and then blue. This cycle should keep repeating itself afterwards. (red-green-blue-red-green-blue...)
I am able to change the colour after the first click with following command:
set(handles.A1_pushbutton,'BackgroundColor','red')
The botton color changes to red after the first click. I am now trying to change the color after the next click to green and after the third click to blue.
My idea was maybe use a switch command that looks something like this:
  • 1 + 3n times clicked --> Button red
  • 2 + 3n times clicked --> Button green
  • 3 + 3n times clicked --> Button blue
The main issue is that dont know how to cause an action depending on the amount of time clicked.
Thanks in advance!!

Answers (1)

Cris LaPierre
Cris LaPierre on 8 Jan 2021
You are going to have to create some sort of flag or variable that keeps track of how many times the button has been pushed. If the counter is a multiple of 3 (1,4,7,...), the color should be set to red. If it is (2,5,8,...) it should be green, etc.
Use that counter as your switch. Then in each case, set the color.
To see how to store data in your gui, see this page.

Categories

Find more on Modeling in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!