1

I am trying to change the background color of toggle buttons depending on my array output. Basically i have a loop which is filling the array.

 number = 20;
tic
A = [];
for i = 1 : number
    A = [i]
    pause(1)
end

Depending on what number i is inside the Array i want to change that background color of the toggle button in my GUI. so i tried with this:

function togglebutton1_Callback(hObject, eventdata, handles)
if A == 1 
    set('BackgroundColor','red')

Sadly it didn't work. I would appreciate any help

spr1te
  • 89
  • 7
  • The background color is not editable since that indicates whether the toggle is pressed or not. You'd likely have to resort to some java for that: http://stackoverflow.com/questions/25078722/jtogglebutton-how-to-change-the-color – Suever Dec 05 '16 at 13:45
  • Hello, in my main project the buttons get values from being toggled and also change the colors, but in the end i get an array as result which shows a way (of button numbers) and i wanted to light that up with color changes per button so it visualizes the way it goes. – spr1te Dec 05 '16 at 13:47
  • As I said, it's not possible directly in MATLAB and you'll likely have to use a Java toggle button instead – Suever Dec 05 '16 at 13:50
  • maybe i misexplained something, i dont want to change the color of my togglebutton when i press any togglebutton... i want to change the togglebutton colors depending on the number inside my array – spr1te Dec 05 '16 at 18:02

2 Answers2

2

It is going to be difficult to change the true background color of the uicontrol (with the style set to 'toggle') since that is regulated by the underlying java object. If you want to change that you'll likely have to resort to something like this

What you could do though, is rely on the fact that MATLAB's uicontrol supports HTML-formatted strings. So you could use HTML to make the button appear to be a different color

set(button, 'String', '<HTML><BODY bgcolor="red">Red Toggle');

enter image description here

Or personally, I think it looks better to simply change the foreground color

set(button, 'ForegroundColor', 'red')

enter image description here

Community
  • 1
  • 1
Suever
  • 64,497
  • 14
  • 82
  • 101
  • i will try this in abit and give u feedback later – spr1te Dec 05 '16 at 14:17
  • @spr1te How are you creating your toggle button? Please show that code – Suever Dec 05 '16 at 18:40
  • i created a static matrix of togglebuttons via guide interface and then imported all (they're all callbacks). inside these callbacks the buttons already change color if pressed once or twice. in the end i get an answer array e.g. [1 2 3 4 5] and i want then the togglebuttons 1-5 to light up in another color (without pressing them) that is my basic problem – spr1te Dec 05 '16 at 19:11
-2

I realise this might not solve your fundamental issue, but keep the following in mind when changing properties.

set works with a reference to the handle graphics object as its first argument, i.e. your button.

set(button,'BackgroundColor','red')

This works for me on R2013a. However, the documentation states that you should only use it for releases before R2014b. For release R2014b and onwards, using dot notation should be the norm:

button = uicontrol('Style','togglebutton');
button.BackgroundColor = 'r';

https://uk.mathworks.com/help/matlab/ref/uicontrol-properties.html#property_BackgroundColor