【问题标题】:how to subplot n figures (pcolor) using for loop (matlab)如何使用for循环(matlab)对n个数字(pcolor)进行子图绘制
【发布时间】:2021-01-15 22:23:29
【问题描述】:

我有 n 个保存的 fig 文件(所有数字都是 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

【问题讨论】:

  • 欢迎来到 Stack Overflow!请使用tour 并阅读How to Ask。目前尚不清楚您希望我们做什么,尤其是您在此处发布的代码的作用。您能否请edit 澄清问题并将代码修改为minimal reproducible example,即一段代码演示我们 可以运行的问题。因此,提供样本输入(可能是两个 .fig 文件,您可以使用随机数据创建这些文件)和样本输出(可能是带有两个子图的图形)。

标签: matlab matlab-figure subplot


【解决方案1】:

您可以使用copyobj 将现有坐标区的内容复制到目标坐标区。解释见 cmets:

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

【讨论】:

  • @SunSer 你有错误吗?什么不起作用?请描述什么不起作用,并且不要在 cmets 中发布代码(根本不可读)。您可以edit您的问题并在那里添加新信息
  • 你能看到我的答案吗?
猜你喜欢
  • 2015-07-24
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多