Search code examples
matlaboctavematlab-figure

Octave/Matlab - Subplotting figures with existing plots


My Octave workflow is the following:

I have tons of data to process, and lots of plots to generate. For each plot, I have a function that does all the work, generates its own plot and returns the handle of this plot for future modifications (if needed). Something like this:

function [h,p] = processData_and_generatePlot_A(datainput)
       %%.....
       h = figure();
       p = plot(h, ...)
       %%....
end

Now, what I'm trying to do is a script calling all this functions, collecting all the figures, and trying to combine all of them in only one figure (i.e., each plot generated should be a subplot in a new figure).

So, the questions are:

  • Is it possible to pass the plot handler to the subplot function, so the plot is printed instead of generate a new one?
  • If not, is there any way to insert existing plots into a new figure?

Thanks in advance


Solution

  • A method for merging plots in different figures as subplot of a new figure actually exists. What afraids me is that you have "lots of plots to generate", so you must define a criterion for splitting the existing plots into N figures in order to avoid cramming all of them into a single figure.

    The aforementioned approach involves the usage of the copyobj function, and here is an example that you can easily modify following your needs:

    f1 = figure();
    x1 = -10:0.1:10;
    y1 = sin(x1);
    p1 = plot(x1,y1,'r');
    
    f2 = figure();
    x2 = -10:0.1:10;
    y2 = cos(x2);
    p2 = plot(x2,y2,'r');
    
    pause(5);
    
    f3 = figure();
    sub1 = subplot(1,2,1);
    sub2 = subplot(1,2,2);
    
    copyobj(p1,sub1);
    delete(f1);
    
    copyobj(p2,sub2);
    delete(f2);