我想迭代地从一个图形中创建子图。我有以下代码:
for i=1:numel(snips_timesteps)
x=openfig(sprintf('./results/snips/temp/%d.fig',i),'reuse');
title(sprintf('Timestep %d',snips_timesteps(i)));
xlabel('');
ax= gca;
handles{i} = get(ax,'children');
close gcf;
end
h3 = figure; %create new figure
for i=1:numel(snips_timesteps)
s=subplot(4,4,i)
copyobj(handles{i},s);
end我得到的错误是:
Error using copyobj
Copyobj cannot create a copy of an
invalid handle.
K>> handles{i}
ans =
3x1 graphics array:
Graphics (deleted handle)
Graphics (deleted handle)
Graphics (deleted handle)是否有可能关闭一个数字,但仍然保留它的句柄?这段话似乎被删除了。
编辑
handles={};
for i=1:numel(snips_timesteps)
x=openfig(sprintf('./results/snips/temp/%d.fig',i),'reuse');
title(sprintf('Timestep %d',snips_timesteps(i)));
xlabel('');
ax= gca;
handles{i} = get(ax,'children');
%close gcf;
end
h3 = figure; %create new figure
for i=1:numel(snips_timesteps)
figure(h3);
s=subplot(4,4,i)
copyobj(handles{i},s);
close(handles{i});
end错误:
K>> handles{i}
ans =
3x1 graphics array:
Line
Quiver
Line
K>> close(handles{i})
Error using close (line 116)
Invalid figure handle.如果我删除close(handles{i}),它会再次为所有子图绘制第一个图形!
发布于 2015-09-25 21:55:55
不是的。通过关闭数字,您将删除它和所有与它相关的数据,包括它的句柄。
如果您移除close gcf;并将figure(i); close gcf;放在copyobj(handles{i},s);之后,您将得到所需的效果。但是,您还需要在figure(h3);之前添加s=subplot(4,4,i),以确保将子图添加到正确的图形中。
下面是一些示例代码,向您展示它的工作原理。它首先创建4个数字和抓住手柄到他们的轴它。然后我们循环遍历每个图形,并将其复制到另一个图形的子图中。
for i = 1:4
figure(i)
y = randi(10, [4 3]);
bar(y)
ax = gca;
handles{i} = get(ax, 'Children');
end
for i = 1:4
figure(5);
s = subplot(2, 2, i);
copyobj(handles{i}, s);
figure(i);
close gcf;
endhttps://stackoverflow.com/questions/32790477
复制相似问题