Matlab-在按下按钮之前更改按钮的颜色

Matlab-在按下按钮之前更改按钮的颜色,matlab,for-loop,Matlab,For Loop,在实际按下按钮之前,我试图更改mat alb中某些按钮的颜色,但只是根据某个矩阵中的值(这些值可能会更改)。下面的代码运行,但结果很奇怪。我对for循环的错误是什么 这是矩阵的一个示例: A = [0 0 3 1 1]; 我在这里设置屏幕: scr = get(0, 'screensize'); f1 = figure(1); set(f1, 'menubar', 'none'); set(f1, 'position', [scr(1) scr(2) scr(3) scr(4)]); 以下是

在实际按下按钮之前,我试图更改mat alb中某些按钮的颜色,但只是根据某个矩阵中的值(这些值可能会更改)。下面的代码运行,但结果很奇怪。我对for循环的错误是什么

这是矩阵的一个示例:

A = [0 0 3 1 1];
我在这里设置屏幕:

scr = get(0, 'screensize');
f1 = figure(1);
set(f1, 'menubar', 'none');
set(f1, 'position', [scr(1) scr(2) scr(3) scr(4)]);
以下是按钮:

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]);
我将它们放在一个数组中:

L = [h1 h2 h3 h4 h5];
现在我想让按钮根据矩阵中的值改变颜色。因此,如果矩阵中的第一个值为零,则第一个按钮的颜色应为白色。如果矩阵中的第二个值为零,则第二个按钮的颜色应为白色,依此类推

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

但最后我只得到了最后一个蓝色按钮,其他按钮没有改变!它们都应该在改变颜色。

for循环只会遍历最后一个索引。换成

for i = 1 : length(A)

它将正确迭代。

欢迎使用StackOverflow。您能否编辑您的问题,以说明您的代码到底做了什么以及您的期望。如果我想以
a=[0 0 3 1,0 0 1 1 0]
的形式遍历一个5 x 2矩阵,而不是一个5 x 1矩阵,它有两行按钮,该怎么办?在这种情况下,我有一个问题,因为a的长度总是5。做一个嵌套for循环,
m=1:size(a,1)
n=1:size(a,2)