MATLAB: How to add a table beneath a plot

axesmultiplemultiple rowsplotplot tabletablextickxticklabel

I want to add a table beneath the the x-axis as shown in the picture.
I have made the plot, but not sure how to add the additional table. This is my code:
B30_R = [0.1305,0.2031,0.2735,0.2584,0.40526,0.5471,0.3835,0.5795,0.8407];
U30_R = [0.1291,0.1429,0.1516,0.2707,0.2861,0.3033,0.4216,0.4420,0.4586];
S30_R = [0.0879,0.1027,0.1131,0.1800,0.2060,0.2262,0.2754,0.3274,0.3473];
V30_R = [0.1351,0.1359,0.1272,0.2904,0.2722,0.2544,0.4609,0.4210,0.3758];
figure()
hold on
plot(V30_R)
plot(U30_R)
plot(S30_R)
plot(B30_R)
xlabel('Sea state')
ylabel('RMS Roll [deg.]')
title('RMS in roll at 30 deg')
legend('Violin','UT540','SWATH','Base case')
hold off

Best Answer

The rows of the table are actually additional x-tick labels.
There are several ways to plot a table under the axes but the cleanest approach would be to create multi-row XTickLabels and all you need are these 6 lines of code. With this method, you don't need to worry about the spacing of your axes or the XLabel.
There are some important details that should be understood before adapting this to your needs. The details are added as footnotes.
xlim([0,9]) % [1]
labelMat= [1:numel(B30_R); B30_R; U30_R; S30_R; V30_R]; % [2]
tickLabels = compose('%5d\\newline%.3f\\newline%.3f\\newline%.3f\\newline%.3f',labelMat(:).'); % [3]
tickDefs = 'Index\newlineB30 R\newlineU30 R\newlineS30 R\newlineV30 R'; % [4]
tickLabels = [tickDefs,tickLabels];
set(gca,'XTick', 0:numel(tickLabels), 'XTickLabel', tickLabels, 'TickDir', 'out') % [5]
Footnotes:
  1. Your x-data span from 1 to 9 but xlim starts at 0 so we have an extra tick on the left for the x-tick-row-names.
  2. 'labelMat' is an n*m numeric matrix for n rows of m tick labels
  3. This is the key line of code to produce the multi-row tick labels. The '%5d' at the beginning is used to space the index integers in row 1 of the x lables so they are centered. The '5' is my estimate of what fits best. The %.3f shows 3 dp for each value in rows 2:5.
  4. This line creates the xtick row definitions on the left of the xtick 'table'.
  5. Make sure the xticks start at 0 since your data starts at 1. This will give space to add the row definitions. Also, notice that the TickDir is 'out' so that the ticks point to the labels. You could also try 'both'.