Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MATLAB中更改按钮可见性?_Matlab - Fatal编程技术网

如何在MATLAB中更改按钮可见性?

如何在MATLAB中更改按钮可见性?,matlab,Matlab,我正在尝试创建一个简单的游戏 它应该做什么:创建两个按钮,当用户点击每个按钮时,它应该消失 它的实际作用:当我点击第一个按钮时,它消失了。但当我点击第二个按钮时,什么也没发生 clear all, clc, close all fh = figure; n = 2; x = ceil(rand(10)*2); y = ceil(rand(10)*2); bgh = uibuttongroup('Parent',fh,'Title',... 'Button Game','Positi

我正在尝试创建一个简单的游戏

它应该做什么:创建两个按钮,当用户点击每个按钮时,它应该消失

它的实际作用:当我点击第一个按钮时,它消失了。但当我点击第二个按钮时,什么也没发生

clear all, clc, close all

fh = figure;

n = 2;
x = ceil(rand(10)*2);
y = ceil(rand(10)*2);

bgh = uibuttongroup('Parent',fh,'Title',...
    'Button Game','Position',[.1 .2 .8 .6]);

for i = 1:n
    rbh1 = uicontrol(bgh,'Style','Pushbutton','String','Red',...
        'Units','normalized','Position',[rand(1) rand(1) x(1,i) y(1,i)]);
    set(rbh1,'CallBack','set(rbh1,''visible'',''off'')')
end

axt = axes('Parent',bgh,'Units','normalized');

axis([0.5 1 0.5 1])
axis square
axis off

如何解决此问题?

问题是您只为一个句柄设置回调。将代码的循环位更改为以下内容,它就会工作。因为这看起来像是一个学习练习,所以我将把它留给您去探索,并找出为什么做出这种改变会有帮助

for i = 1:n
rbh(i) = uicontrol(bgh,'Style','Pushbutton','String','Red',...
'Units','normalized','Position',[rand(1) rand(1) x(1,i) y(1,i)]);

set(rbh(i),'CallBack',['set(rbh( ' num2str(i) '),''visible'',''off'')'])
end