%% Add Legend to Specific Axes
% Starting in R2019b, you can display a tiling of plots using the tiledlayout and nexttile functions. Call the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes objects ax1 and ax2. Plot random data in each axes. Add a legend to the upper plot by specifying ax1 as the first input argument to legend.

tiledlayout(2,1)
y1 = rand(3);
ax1 = nexttile; 
plot(y1)

y2 = rand(5);
ax2 = nexttile; 
plot(y2)

legend(ax1,{'Line 1','Line 2','Line 3'})

% Copyright 2019 The MathWorks, Inc.