Changing color of pushbuttons before pushing them

조회 수: 9 (최근 30일)
Serena De Stefani
Serena De Stefani 2015년 12월 3일
답변: Serena De Stefani 2015년 12월 3일
I am trying to change the colors of some pushbuttons in mat alb before actually pushing them, but just according to the values in a certain matrix (these values may change). The following code runs but the result is odd. What I am doing wrong with the for loop?
This is an example of matrix:
A = [0 0 3 1 1];
Here I set the screen:
scr = get(0, 'screensize');
f1 = figure(1);
set(f1, 'menubar', 'none');
set(f1, 'position', [scr(1) scr(2) scr(3) scr(4)]);
These are the pushbuttons:
h1 = uicontrol('Style', 'pushbutton',...
'BackgroundColor', '[0.91 0.91 0.91]',...
'Position', [200 200 100 100]);
h2 = uicontrol('Style', 'pushbutton',...
'BackgroundColor', '[0.91 0.91 0.91]',...
'Position', [300 200 100 100]);
h3 = uicontrol('Style', 'pushbutton',...
'BackgroundColor', '[0.91 0.91 0.91]',...
'Position', [400 200 100 100]);
h4 = uicontrol('Style', 'pushbutton',...
'BackgroundColor', '[0.91 0.91 0.91]',...
'Position', [500 200 100 100]);
h5 = uicontrol('Style', 'pushbutton',...
'BackgroundColor', '[0.91 0.91 0.91]',...
'Position', [600 200 100 100]);
I put them together in an array:
L = [h1 h2 h3 h4 h5];
Now I want the pushbuttons to change color according to the values in the matrix. So if the first value in the matrix is zero, the color of the first pushbutton should be white. If the second value in the matrix is zero, the color of the second pushbutton should be white, and so on.
for i = length(A)
if A(i) == 0
set(L(i),'Backgroundcolor', 'w');
elseif A(i) == 1
set(L(i),'Backgroundcolor','b');
elseif A(i) == 2
set(L(i),'Backgroundcolor','y');
elseif A(i) == 3
set(L(i),'Backgroundcolor','g');
end
end
But in the end I just get the last pushbutton blue, and the others unchanged! They should all be changing color.

채택된 답변

Serena De Stefani
Serena De Stefani 2015년 12월 3일
I changed the parameter to numeric but that did not solve the problem. I have found out that the for loop only goes over the last index. I also need to change the loop indication to:
for i = 1 : length(A)
and it will iterate correctly.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 12월 3일
h1 = uicontrol('Style', 'pushbutton',...
'BackgroundColor', [0.91 0.91 0.91],...
'Position', [200 200 100 100]);
h2 = uicontrol('Style', 'pushbutton',...
'BackgroundColor', [0.91 0.91 0.91],...
'Position', [300 200 100 100]);
h3 = uicontrol('Style', 'pushbutton',...
'BackgroundColor', [0.91 0.91 0.91],...
'Position', [400 200 100 100]);
h4 = uicontrol('Style', 'pushbutton',...
'BackgroundColor', [0.91 0.91 0.91],...
'Position', [500 200 100 100]);
h5 = uicontrol('Style', 'pushbutton',...
'BackgroundColor', [0.91 0.91 0.91],...
'Position', [600 200 100 100]);
That is, the parameter has to be numeric [0.91 0.91 0.91] rather than a string '[0.91 0.91 0.91]'

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by