MATLAB: Hey everyone, does anyone know how to make a pushbutton in GUI, when it’s pushed, change to green or red depending on whether the text/ question on it is right or wrong

guipushbuttonr2007b

I have created my user interface and have a question written on my pushbuttons asking, for example, Are the sensors correct or incorrect? I want the user to be able to push the button and have the button turn green if the are correct and turn red if they are incorrect. Thanks for the help in advance!

Best Answer

Changing the color of a button can be done in the button's callback:
function button1_Callback(hObject, EventData)
if AreCorrect()
set(hObject, 'Color', [0,1,0]);
else
set(hObject, 'Color', [1,0,0]);
end
end
Now it matters, what "if they are correct" exactly means. What should trigger the change of the color exactly? I've simplified it here as "AreCorrect()" because I cannot guess, what you exactly want.