将数字合并为一个新数字

时间:2016-03-20 15:00:23

标签: matlab matlab-figure

enter image description here

我在链接上获得了很多数字。在每个图中,我都有两张图。我想从每个图中取出左图并将它们组合成一个新图。对于前我有3个数字。我将获得一个仅包含3个数字的左图的数字。最后,我将有一个图中有3个左图。

1 个答案:

答案 0 :(得分:2)

您可以使用copyobj来完成此操作。您需要将每个axes对象从旧图复制到新图。此外,由于图例与轴分开,您还需要重新创建它们。以下脚本应该能够实现此目的。您只需要提供每个原始图形中的子图数(默认为2)和要复制的图形句柄数组。

figures = [fig1, fig2];    
nSubplots = 2;

% Create all of the new figure that we're going to copy into
for k = 1:nSubplots
    newfig(k) = figure();
end

% For each existing figure, copy all axes
for k = 1:numel(figures)

    % Find the legends
    legends = findall(figures(k), 'tag', 'legend');

    % Sort by x position so we know which goes with which axes
    positions = get(legends, 'Position');
    positions = cat(1, positions{:});
    [~, sortind] = sort(positions(:,1));
    legends = legends(sortind);

    % Now we want to copy each axes in this figure
    for n = 1:nSubplots
        % Retrieve the axes handle
        ax = subplot(1,nSubplots,1, 'Parent', figures(k));

        % Determine the position of the axes in the new figure
        s = subplot(1, numel(figures), k, 'Parent', newfig(n));
        pos = get(s, 'Position');
        delete(s)

        % Copy old axes over to this figure
        newax = copyobj(ax, newfig(n));
        set(newax, 'Position', pos);

        % Copy the legend from the old plot as well
        legend(newax, get(legends(n), 'string'))
    end
end