我试图以编程的方式制作MATLAB,并面对使用滑块后消失的问题,。我隔离了这个问题,以保持代码简短。在这个GUI中,我希望每次使用滑块时都刷新plotmatrix (忽略滑块的值与我的程序完全无关的事实,正如前面提到的那样,我确实希望保持代码的干净,这就是为什么我也删除了这个功能)。下面是代码(您必须将其作为函数运行):
function StackOverflowQuestion_GUI()
% clear memory
close all; clc;
% initialize figure
f = figure;
% create main axes
AX_main = axes('Parent',f,...
'Units','normalized','Position',[.1 .2 .8 .7]);
% create slider
uicontrol('Parent',f,...
'Style','slider','Callback',{@sliderCallback,AX_main},...
'Units','normalized','Position',[0.05 0.05 0.9 0.05]);
plotmatrix(AX_main,randn(500,3));
title('Random Plotmatrix');
end
function sliderCallback(~,~,AX_main) % callback for slider
plotmatrix(AX_main,randn(500,3));
title('Random Plotmatrix NEW');
end任何帮助都是非常感谢的!我想我误解了斧头的概念。当我绘制到我创建的轴柄时,为什么图形的其他部分也会受到影响?如果有人能向我解释一下这个图形处理系统是如何工作的,那就太好了!
发布于 2017-03-24 15:50:44
调用plotmatrix时,函数将完全重新绘制图形,以保存其他元素,应使用hold on; hold off;语句:
function StackOverflowQuestion_GUI()
% clear memory
clear; close all; clc;
% initialize figure
f = figure;
% create main axes
AX_main = axes('Parent',f,...
'Units','normalized','Position',[.1 .2 .8 .7]);
% create slider
uicontrol('Parent',f,...
'Style','slider','Callback',{@sliderCallback,AX_main},...
'Units','normalized','Position',[0.05 0.05 0.9 0.05]);
plotmatrix(AX_main,randn(500,3));
title('Random Plotmatrix');
end
function sliderCallback(~,~,AX_main) % callback for slider
hold on;
plotmatrix(AX_main,randn(500,3));
hold off;
title('Random Plotmatrix NEW');
end发布于 2017-03-24 17:40:13
虽然daren shan's answer是正确的,但这是非常奇怪的行为,我好奇地想知道背后是什么。
通过plotmatrix的源代码,我们可以找到删除滑块对象的行:
% Create/find BigAx and make it invisible
BigAx = newplot(cax);这里没有明显的东西,newplot是做什么的?
在高级图形代码的开头使用
newplot来确定图形输出的目标图形和轴。调用newplot可以更改当前图形和当前轴。基本上,在用现有的图形和轴绘制图形时,有三种选择:
哦..。
因此,newplot正在删除滑块对象。
那么,为什么hold会阻止删除滑块,尽管它是一个轴方法而不是一个图形方法?首先,看看文档中的“算法”主题:
hold函数将Axes或PolarAxes对象的NextPlot属性设置为'add'或'replace'。
因此,hold on为当前轴将其设置为'add'。但是,由于我目前还不知道的原因,这也将图形的NextPlot设置为add。
我们可以通过一个简短的片段看到这一点:
f = figure('NextPlot', 'replacechildren');
ax = axes;
fprintf('NextPlot Status, base:\nFig: %s, Ax(1): %s\n\n', f.NextPlot, ax.NextPlot)
hold on
fprintf('NextPlot Status, hold on:\nFig: %s, Ax(1): %s\n\n', f.NextPlot, ax.NextPlot)其中的指纹:
NextPlot Status, base:
Fig: replacechildren, Ax(1): replace
NextPlot Status, hold on:
Fig: add, Ax(1): add奇怪的行为,但我不想再多说了。
这有什么关系?回到newplot文档。首先,newplot读取图形的NextPlot属性以确定要做什么。默认情况下,图形的NextPlot属性设置为'add',因此它将保留当前的所有图形对象,但是plotmatrix显式地更改了以下内容:
if ~hold_state
set(fig,'NextPlot','replacechildren')
end因此,newplot从:
绘制到当前图形,而不清除任何已经存在的图形对象。
至:
删除
HandleVisibility属性设置为on的所有子对象,并将图NextPlot属性重置为add。 这将清除当前的数字,并相当于发出clf命令。
这解释了为什么滑块消失和为什么hold on修复了这个问题。
根据newplot的文档,我们还可以设置滑块UIcontrol的HandleVisibility以避免被销毁:
% create slider
uicontrol('Parent',f,...
'Style','slider','Callback',{@sliderCallback,AX_main},...
'Units','normalized','Position',[0.05 0.05 0.9 0.05], ...
'HandleVisibility', 'off');https://stackoverflow.com/questions/42997739
复制相似问题