Producing subplots and then combine them into a figure later in MATLAB

前端 未结 4 1014
感动是毒
感动是毒 2020-12-01 16:59

My program produces small figures during the command cycle. Is there a way to just save these figures and then combine them in one figure later?

相关标签:
4条回答
  • 2020-12-01 17:37

    Consider the code:

    hFig = figure;
    
    %# create temporary subplots as template
    for i=1:2, h(i) = subplot(2,1,i); end       %# create subplots
    pos = get(h, 'Position');                   %# record their positions
    delete(h)                                   %# delete them
    
    %# load the .fig files inside the new figure
    fileNames = {'a.fig' 'b.fig'};              %# saved *.fig file names
    for i=1:2
        %# load fig
        hFigFile = hgload( fileNames{i} );
    
        %# move/copy axis from old fig to new fig
        hAx = get(hFigFile, 'Child');           %# hAx = gca;
        set(hAx, 'Parent',hFig)
        %#hAx = copyobj(hAx,hFig);
    
        %# resize it to match subplot position
        set(hAx, 'Position',pos{i});
    
        %# delete old fig
        delete(hFigFile)
    end
    

    This was adapted from this newsgroup discussion

    0 讨论(0)
  • 2020-12-01 17:38

    Amro's solution works greatly, but with boxplots you have to reset the Xtick and Xtick labels, otherwise, for some reason, they will not be resized according to the the subplot. When you create the boxplot or after opening the figure add:

    set(gca,'XTick',<1d vector>,'XTickLabel',<1d cell vector>)
    

    or put automatic ticks and labels

    set(gca,'XTickMode','auto','XTickLabelMode','auto')
    
    0 讨论(0)
  • 2020-12-01 17:41

    Use saveas. Save your subplot as a FIG file so you have complete control over it later (as opposed to a JPG).

    Choose a tiling pattern and then use subplot to display multiple figures in one.

    0 讨论(0)
  • 2020-12-01 17:49

    I have an answer here as an example:

    h1 = figure(1)
    plot(1:10,'o-r');
    title('title');
    xlabel('xlabel');
    ylabel('ylabel');
    
    % Copy contents
    ch(1) = copyobj(gca,gcf);
    
    % Figure 2
    h2 = figure(2)
    plot(1:30,'o-r');
    title('title fig2');
    xlabel('xlabel');
    ylabel('ylabel');
    % copy contents
    ch(2) = copyobj(gca,gcf);
    
    figure(3)
    sh = subplot(1,2,1);
    clear axes
    p = get(sh,'position');
    ah = copyobj(ch(1),gcf);
    set(ah,'position',p);
    
    % Create axis template
    sh = subplot(1,2,2);
    clear axes
    p = get(sh,'position');
    ah = copyobj(ch(2),gcf);
    set(ah,'position',p);
    
    % Delete template
    % delete(sh);
    
    0 讨论(0)
提交回复
热议问题