Toggle button background color unable to change

9 Ansichten (letzte 30 Tage)
Maury Lander-Portnoy
Maury Lander-Portnoy am 10 Mai 2015
Hi, I'm trying to a make a GUI where the default background color of a toggle button is black ([0 0 0]) and when it becomes pressed the background color changes to gray ([.5 .5 .5]). The text string in the button will be white but I've had no problems changing this so I'll put it aside for this question. I have tried both in 'guide' and in the actual code to specify the background color and its change but so far I have had no luck. Here's the code:
% --- Executes on button press in togglebutton11.
function togglebutton11_Callback(hObject, eventdata, handles)
if get(hObject,'value') == 1
set(hObject,'Backgroundcolor',[.5 .5 .5])
elseif get(hObject,'value') == 0
set(hObject,'Backgroundcolor',[0 0 0])
end
% hObject handle to togglebutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton11
% --- Executes during object creation, after setting all properties.
function togglebutton11_CreateFcn(hObject, eventdata, handles)
set(hObject,'Backgroundcolor',[0 0 0])
% hObject handle to togglebutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
I have tried this same code with a push button equivalent (obviously without the 'value' variance) and it seems to work fine so I suppose MATLAB disables users from setting their own background color values for toggle buttons? HELP!
  1 Kommentar
Adam
Adam am 11 Mai 2015
Are both parts not working or just the callback which changes the colour?

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Joseph Cheng
Joseph Cheng am 10 Mai 2015
Well i setup a simple example gui using your code and your code works for me. Things i would try is shut down matlab and try again. if problem still occurs then i would put a break point using the debugger at that if statement. then in the command window see what the values are and whether the sets are being conducted. One probably obvious issue is that togglebutton11 might not be the toggle button you are pushing. A probably dumb suggestion but it wouldn't be the first time it has come up and i couldn't say i haven't done the same when dealing with 11 controls just labeled 1 through 11.
  3 Kommentare
Joseph Cheng
Joseph Cheng am 11 Mai 2015
R2014a on PC. Haven't had time to think about this more, but definitely do try what i suggested in putting a debugging breakpoint in there and see when if ever it does get set. So keeping the GUI on one side of the screen or perhaps if you have 2 screen on the other. Step through the color changing section. After the line in which is should have changed, see if it changed if not query and see what the color is set to or try to set it manually in the command window.
Walter Roberson
Walter Roberson am 11 Mai 2015
A) Try adding
else
fprintf(2, 'callback object value was not 1 or 0, was %g\n', get(hObject,'Value'));
B) try adding
drawnow()

Melden Sie sich an, um zu kommentieren.


Alfonso Nieto-Castanon
Alfonso Nieto-Castanon am 11 Mai 2015
Bearbeitet: Alfonso Nieto-Castanon am 11 Mai 2015
I would first check if you are able to change the color of the togglebutton manually, e.g.
h = uicontrol('style','togglebutton','string','test');
set(h,'backgroundcolor','k');
If that does not work, then this is a java-related issue (some LAFs limit Matlab's ability to modify togglebutton's and popupmenu's bakground colors), and the only workaround that I know of is to change the Look-And-Feel of your uicontrol objects, for example using:
javax.swing.UIManager.setLookAndFeel('javax.swing.plaf.metal.MetalLookAndFeel')
(you may change the LAF of each object separately or globally for your entire gui, see Yair's undocumented matlab for additional info)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by