Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I move several existing plots to one subplot in MATLAB?

Tags:

plot

matlab

I have a function, myFunkyFigure, that takes in data, does some funky things, and returns an axis object for the figure it produces.

I would like to be able to invoke this function twice, creating two different figures:

fig(1) = myFunkyFigure(dataSet1);
fig(2) = myFunkyFigure(dataSet2);

Then I would like to put them into a subplot together.

Note that, because of the funkiness of myFunkyFigure, the following does not work.

subplot(2,1,1);
myFunkyFigure(dataSet1);
subplot(2,1,2);
myFunkyFigure(dataSet2);

I believe that I need something along the lines of copyobj, but I haven't been able to get that to work (I tried following a solution in Stack Overflow question Producing subplots and then combine them into a figure later in MATLAB but to no avail).

like image 480
PengOne Avatar asked Apr 27 '11 17:04

PengOne


People also ask

How would you plot multiple graphs in Matlab?

To create a plot that spans multiple rows or columns, specify the span argument when you call nexttile . For example, create a 2-by-2 layout. Plot into the first two tiles. Then create a plot that spans one row and two columns.

What is subplot MNP Matlab?

Description. example. subplot( m , n , p ) divides the current figure into an m -by- n grid and creates axes in the position specified by p . MATLAB® numbers subplot positions by row. The first subplot is the first column of the first row, the second subplot is the second column of the first row, and so on.

Can Matlab have multiple third plots?

You can display multiple plots in different parts of the same window using either tiledlayout or subplot .


1 Answers

Obviously, we don't know how "funky" your figures are, but it should be noted in such a case that the cleanest solution would be to modify the function myFunkyFigure such that it accepts additional optional arguments, specifically the handle of an axes in which to place the plot it creates. Then you would use it like so:

hSub1 = subplot(2,1,1);         %# Create a subplot
myFunkyFigure(dataSet1,hSub1);  %# Add a funky plot to the subplot axes
hSub2 = subplot(2,1,2);         %# Create a second subplot
myFunkyFigure(dataSet2,hSub2);  %# Add a funky plot to the second subplot axes

The default behavior of myFunkyFigure (i.e. no additional arguments specified) would be to create its own figure and place the plot there.

However, to answer the question you asked, here's a way to accomplish this given that you are outputting the axes handles (not the figure handles) in the vector fig (note: this is basically the same solution as the one given in the other question, but since you mention having trouble adapting it I thought I'd reformat it to better fit your specific situation):

hFigure = figure();                              %# Create a new figure
hTemp = subplot(2,1,1,'Parent',hFigure);         %# Create a temporary subplot
newPos = get(hTemp,'Position');                  %# Get its position
delete(hTemp);                                   %# Delete the subplot
set(fig(1),'Parent',hFigure,'Position',newPos);  %# Move axes to the new figure
                                                 %#   and modify its position
hTemp = subplot(2,1,2,'Parent',hFigure);         %# Make a new temporary subplot
%# ...repeat the above for fig(2)

The above will actually move the axes from the old figure to the new figure. If you want the axes object to appear in both figures, you can instead use the function COPYOBJ like so:

hNew = copyobj(fig(1),hFigure);  %# Copy fig(1) to hFigure, making a new handle
set(hNew,'Position',newPos);     %# Modify its position

Also note that SUBPLOT is only used here to generate a position for the tiling of the axes. You could avoid the need to create and then delete subplots by specifying the positions yourself.

like image 160
gnovice Avatar answered Sep 28 '22 06:09

gnovice