MATLAB: How to have multiple plots in a figure with preset height and compact spacing

axeschart;compactfigurefixedheightlayoutMATLABmultipleplotspresetspacingsubplottiled

I want to have 2 plots in a figure stacked vertically with preset height (both 3cm) and compact spacing between them. I am using a tiled chart layout since it has the option for compact spacing, but when I try to set the height using the "Position" argument, I get the following warning.
Warning: Unable to set 'Position', 'InnerPosition', 'OuterPosition', or 'ActivePositionProperty' for objects in a TiledChartLayout
On maximizing/minimizing the generated figure window, it is observed that the height of the axes is not set to the constant value as required. My code is given below:
 
tiledlayout(2,1);
nexttile;
plot(1:10);
ax=gca;
ax.Units='centimeters';
ax.Position(4)=3;
nexttile;
plot(1:20);
ax=gca;
ax.Units='centimeters';
ax.Position(4)=3;

Best Answer

In order to have multiple plots with compact spacing and preset height, you can have separate axes objects in the same figure and set their positions as required. Please take a look at the example below which illustrates this. Here, instead of having a tiled chart layout or subplot, two separate axes objects are created and their positions are specified explicitly.
 
f = figure();
ax1 = axes(f);
ax2 = axes(f);
ax1.Units = 'centimeters';
ax2.Units = 'centimeters';
axHeight = 3;
ax1.Position(4) = axHeight;
ax2.Position(4) = axHeight;
plot(ax2,1:10,1:10);
plot(ax1,1:20,1:20);
spacing = 0.3;%change this to obtain compactness
% position ax2 above ax1 with some spacing. using TightInset will account for any axes labels.
ax2.Position(2) = ax1.Position(2) + ax1.Position(4) + ax1.TightInset(4) + ax2.TightInset(2) + spacing ;