MATLAB: How to cause a change in the colour of a push button every time it is pressed

MATLABmatlab guipush button

how to insert a pushbutton in GUI that can change its color everytime the pushbutton is pressed. I need it to switch its color to another color
so for example: I need in first push change to green in second push into orange and after 6 colors have been placed i need it to repeat the same series of color back again after all 6 colors specified has been displayed then need to save its state. so how can i do that please ? .Thanks in advance.
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

Best Answer

function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
colors = [ 1 0 0; 0 1 0; 0 0 1] %this will be your array of colors
if hObject.UserData == size(colors,1)
hObject.UserData = 0 %if it reaches the end, reset
end
hObject.UserData = hObject.UserData + 1; %stores this data within the button itself
state = hObject.UserData;
hObject.BackgroundColor = colors(state,:);
%note: ForegroundColor is the color of the string, BackgroundColor is the color of the button
make sure you set the 'UserData property of your button to 0 for its initial state, example :
button = uicontrol('style','pushbutton',...
'callback',@pushbutton1_Callback,...
'UserData',0)
In this example, button will turn from red, green, blue, then the cycle repeats.
Can you be clear as to what you mean by 'saves its state?'