首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用数字绘制子图

用数字绘制子图
EN

Stack Overflow用户
提问于 2015-09-25 20:51:21
回答 1查看 827关注 0票数 0

我想迭代地从一个图形中创建子图。我有以下代码:

代码语言:javascript
复制
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

我得到的错误是:

代码语言:javascript
复制
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)

是否有可能关闭一个数字,但仍然保留它的句柄?这段话似乎被删除了。

编辑

代码语言:javascript
复制
    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

错误:

代码语言:javascript
复制
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}),它会再次为所有子图绘制第一个图形!

EN

回答 1

Stack Overflow用户

发布于 2015-09-25 21:55:55

不是的。通过关闭数字,您将删除它和所有与它相关的数据,包括它的句柄。

如果您移除close gcf;并将figure(i); close gcf;放在copyobj(handles{i},s);之后,您将得到所需的效果。但是,您还需要在figure(h3);之前添加s=subplot(4,4,i),以确保将子图添加到正确的图形中。

下面是一些示例代码,向您展示它的工作原理。它首先创建4个数字和抓住手柄到他们的轴它。然后我们循环遍历每个图形,并将其复制到另一个图形的子图中。

代码语言:javascript
复制
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;
end
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32790477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档