how to cause a change in the colour of a push button every time it is pressed ?

19 ビュー (過去 30 日間)
Dany Majed
Dany Majed 2019 年 2 月 26 日
コメント済み: Robin Stengl 2019 年 12 月 9 日
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)

採用された回答

Kevin Phung
Kevin Phung 2019 年 2 月 26 日
編集済み: Kevin Phung 2019 年 2 月 26 日
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?'
  7 件のコメント
Kevin Phung
Kevin Phung 2019 年 2 月 27 日
happy to have helped!
Robin Stengl
Robin Stengl 2019 年 12 月 9 日
Hey guys, i have the same problem with the length of the vector. And I have not got the line : hObject.UserData =0
what should i do now?
And where should i place the button = ... in my gui
Thank you for your help

サインインしてコメントする。

その他の回答 (1 件)

Steve Kulakowski
Steve Kulakowski 2019 年 2 月 26 日
編集済み: Steve Kulakowski 2019 年 2 月 26 日
Hello,
The way in which I know how to do it is as follows. You may have to tweak the code a little before it works properly, but the general idea is that you need a global variable outside of your function call. The if/else statement will toggle everytime the pushbutton is called. Also I'm sure there are other/better ways to do this, but this is the only way I know how.
Best of luck!
global control
control = 1
function pushbutton1_Callback(hObject, eventdata, handles)
global control
if control == 1
set(handles.pushbutton,'BackgroundColor',[0 1 0]);
control = 0
else
set(handles.button_connect_s1,'BackgroundColor',[1 0 0]);
control = 1
end
end
  2 件のコメント
Stephen23
Stephen23 2019 年 2 月 26 日
It is recommended to use guidata rather than a global variable.
Dany Majed
Dany Majed 2019 年 2 月 26 日
編集済み: Dany Majed 2019 年 2 月 26 日
i do quiet understand what are you trying to say but if you realize after the first if statement which is (if control == 1) the program will turn the button into the other color and will end the process after that so in the other pushes of the button the button will remain same color and i am still a beginner so can't tweak the code that much so if you could please help me with that.Thanks in Advance.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by