Search code examples
matlabplotmatlab-figureyaxis

How to add a spanning ylabel on tiledlayout plots?


I've got a tiled layout in MATLAB with 3 tiles and I want to add a vertical label left to the y-axis, spanning over all tiles.

figure('units','normalized','outerposition',[0 0 0.4 0.91])
tlo = tiledlayout(3,1,'TileSpacing','none','Padding','none');
nexttile
set(gca,'XColor','none')
hold on
plot(x1)
hold off
nexttile
set(gca,'XColor','none')
hold on
plot(x2)
hold off
nexttile
hold on
plot(x3)
hold off

Solution

  • As the documentation on tiledlayout() tells you:

    title(t,'Size vs. Distance')
    xlabel(t,'Distance (mm)')
    ylabel(t,'Size (mm)')
    

    generates spanning axis labels and titles. In your case ylabel(tlo,'Your Y label');


    Two style notes:

    • if you're only plotting a single plot, there's no need to hold on;hold off every plot. Also hold off is only necessary if at some point you no longer want to hold the plot, i.e. when you want to overwrite its contents.

    • set(gca, __) has been superseded by OOP style syntax. Using t1 = nexttile; t1.XColor = 'none' makes for cleaner and supposedly faster code.