【发布时间】:2015-12-02 22:33:36
【问题描述】:
我的程序创建了 5 个按钮,我需要根据矩阵 A 中的值更改它们的颜色。所以我在 main 中定义 A,然后在 main 结束时调用函数 create_maze(A)。
以下是create_maze(A):
function create_maze(A)
A
% create 5X5 pushbuttons
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',...
'ForegroundColor', 'blue',...
'Position', [200 200 100 100],...
'Callback', pushbutton1_Callback);
h2 = uicontrol('Style', 'pushbutton',...
'ForegroundColor', 'blue',...
'Position', [300 200 100 100],...
'Callback', pushbutton1_Callback);
h3 = uicontrol('Style', 'pushbutton',...
'ForegroundColor', 'blue',...
'Position', [400 200 100 100],...
'Callback', pushbutton1_Callback);
h4 = uicontrol('Style', 'pushbutton',...
'ForegroundColor', 'blue',...
'Position', [500 200 100 100],...
'Callback', pushbutton1_Callback);
h5 = uicontrol('Style', 'pushbutton',...
'ForegroundColor', 'blue',...
'Position', [600 200 100 100],...
'Callback', pushbutton1_Callback);
function [L] = pushbutton1_Callback(h0object, A)
% here I put the pushbuttons together in an array
L=[h1, h2, h3, h4, h5];
for i = 1:size(A,1)
if i == 0
set(L{i},'Backgroundcolor','w');
elseif i == 1
set(L{i},'Backgroundcolor','b');
elseif i == 2
set(L{1},'Backgroundcolor','g');
elseif i == 2
set(L{i},'Backgroundcolor','y');
end
end
end
end
不幸的是我得到了:
Error using create_maze/pushbutton1_Callback (line 164)
Not enough input arguments.
有人可以帮我解决这个循环吗? 谢谢!
【问题讨论】:
-
你没有在
pushbutton1_Callback中输出任何东西,因此L完全没有被使用。尝试设置function [L]=pushbutton1_Callback(h0object, A) -
谢谢,我试过了,但上面写着
Undefined function or variable "h1" -
嗯,这是一个明确的信息,不是吗?定义
h1。我猜它来自您的h0object,但您需要明确告诉 MATLAB。 -
谢谢,
h1不是已经定义为页面顶部的 GUI 对象了吗? -
不在你的函数内部...在这种情况下将它传递给函数。
标签: matlab loops matlab-figure