Changing color of pushbuttons before pushing them
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 3 de Dic. de 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]'
Categorías
Más información sobre MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!