Changing color of pushbuttons before pushing them
1 view (last 30 days)
Show older comments
Serena De Stefani
on 3 Dec 2015
Answered: Serena De Stefani
on 3 Dec 2015
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.
0 Comments
Accepted Answer
More Answers (1)
Walter Roberson
on 3 Dec 2015
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]'
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!