如何使用for循环(在Matlab中)创建n个图形的子图(pcolor)

3

我有n个保存的图形文件(所有图形都是pcolor图形),我想在一个新的子图中绘制所有图形(n X 2)。能否有人帮帮我?我将非常感激:)

    % Open each figure and copy content
    for i = 1:nFig
        % Open fig-i
        filename = fullfile(rep,list(i).name);
        fighand =  openfig(filename,'invisible');

            h=subplot(nFig,2,i);


        % Close fig-i
        close(fighand)
    end

1
欢迎来到Stack Overflow!请参观[tour]并阅读[ask]。目前不清楚您希望我们做什么,特别是您在此处发布的代码是用来做什么的。您能否[edit]问题以澄清并修改代码为[mcve],即提供一个演示问题的代码片段,我们可以运行。因此,请提供样本输入(可能是两个.fig文件,您可以使用随机数据创建这些文件)和样本输出(可能是具有两个子图的图形)。 - Adriaan
1个回答

0

您可以使用copyobj将现有坐标轴的内容复制到目标坐标轴中。请参见注释以获取说明:

N = 4;

%% generate some figs
fig = cell(1,N);

for k = 1:N
    fig{k} = figure(k);
    fig{k}.Name = sprintf('fig_%i', k);
    pcolor(rand(10));
    
    xlabel(sprintf('some xlabel %i',k))
    ylabel(sprintf('some ylabel %i',k))
    savefig(fig{k}, fig{k}.Name)
end


%% load figs and make nice subplot
fig_all = figure(99);clf;
for k = 1:N
    % load figure
    fig_tmp = openfig(sprintf('fig_%i', k),'invisible');
    % set fig_all as the current figure again
    figure(fig_all);
    % create target axes
    ax_target = subplot(N/2,2,k);
    pcolor(ax_target, rand(5)*0.001); cla; % plot some random pcolor stuff to setup axes correctly

    % get the source data and copy to target axes
    ax_src = fig_tmp.Children;
    copyobj(ax_src.Children, ax_target);
    % set axes properties of target to those of the source
    ax_target.XLim = ax_src.XLim;
    ax_target.YLim = ax_src.YLim;
    
    % copy the axes labels
    ax_target.XLabel.String = ax_src.XLabel.String;
    ax_target.YLabel.String = ax_src.YLabel.String;
end

enter image description here


@SunSer,你是否遇到了错误?哪些部分无法正常工作?请描述一下具体的问题,不要在评论中贴代码(完全无法阅读)。你可以编辑你的问题并在那里添加新信息。 - rinkert
你能看到我的答案吗? - SunSer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接