1
votes

I am attempting to plot a subplot within another subplot in MATLAB. The problem is that the final subplot shows only portions of the smaller subplot. After some searching on the web, it seems one option here is to save the subplot into a temporary figure and add it to the final subplot after. Another way is to manually specify positions of the plots inside the final figure i.e. rolling your own subplot. I was wondering if there were a more elegant approach. Apologies in advance if this isn't possible/desirable behavior. My idea of how the code would run is:

someData = linspace(0,10);
subplot(2,2,1);
plotThing(someData);
subplot(2,2,2);
plot(cos(someData));

function [ out ] = plotThing( someData )
   y1 = sin(someData);
   y2 = sin(2*someData);
   y3 = sin(4*someData);
   y4 = sin(8*someData);
   f = figure(4);

   subplot(2,2,1)
   plot(someData,y1)
   subplot(2,2,2)
   plot(someData,y2)
   subplot(2,2,3)
   plot(someData,y3)
   subplot(2,2,4)
   plot(someData,y4)

   out = f;
end

My desired behavior would be to have two things in the final figure, the subplot of four items on top, and the single plot underneath. Any advice would be great.

EDIT - The issue with the accepted answer is that it would require a significant number of subplots (12x12 in my situation). After some thought and work, I discovered that it is possible to encapsulate the contents of a subplot into a uipanel. This gives the desired behavior of a subplot being able to be treated as a unit in a larger scale figure. The code to accomplish this looks something like:

plotThing(someData, [0, 0.5, 1, 0.5]);
panel2 = uipanel('Position', [0, 0, 1, 0.5]);
subplot(1,1,1,'Parent', panel2); %Strange but necessary as one cannot set the parent of a plot directly
plot(cos(someData));

function [ out ] = plotThing( someData, position )
    panel = uipanel('Position', position); 

    y1 = sin(someData);
    y2 = sin(2*someData);
    y3 = sin(4*someData);
    y4 = sin(8*someData);

    subplot(2,2,1, 'Parent', panel)
    plot(someData,y1)
    subplot(2,2,2, 'Parent', panel)
    plot(someData,y2)
    subplot(2,2,3, 'Parent', panel)
    plot(someData,y3)
    subplot(2,2,4, 'Parent', panel)
    plot(someData,y4)
end

The result of this generalized solution look like: results

3

3 Answers

2
votes

You should be able to do this by telling MATLAB the span of your subplots as follows.

subplot(4,2,1);  % create a plot with subplots in a grid of 4 x 2
plot(someData,y1); % subplot at first row, first column
subplot(4,2,2);
plot(someData,y2); % subplot at first row, second column
subplot(4,2,3);
plot(someData,y3);
subplot(4,2,4);
plot(someData,y4);
subplot(4,2,[5 6 7 8]); % subplot spanning the entire third and fourth row
plot(someData,y5);      % change [5, 6, 7, 8] to change the span
0
votes

You would still use the subplot command, just use a vector for the position if you want it to take up more than 1 space:

subplot(2,4,1)
plot(someData,y1)
subplot(2,4,2)
plot(someData,y2)
subplot(2,4,3)
plot(someData,y3)
subplot(2,4,4)
plot(someData,y4)

subplot(2,4,[5:8])
plot(bigPlot,y5)

should do it.

0
votes

Using a fine grid and spanning bigger plots over multiple fields can be avoided: subplot can be called with different grid sizes even within the same figure. But the subplots in the smallest grid should be drawn first, otherwise I experienced some overlap (in Matlab 2017). You just have to make sure, that your grids fit together.

For example:

subplot(4,2,1)
plot(someData)
subplot(4,2,3)
plot(someData)
subplot(2,2,2)
plot(someData)
subplot(2,2,3)
plot(someData)
subplot(2,2,4)
plot(someData)