You can create an overall legend by first using 'tiledlayout' to create your subplots. Then, generate the legend and specify what should appear in the legend using a vector of graphics object handles. The position of the legend can be set by its 'Layout' property. Find an example of this workflow below.
figure()
tcl = tiledlayout(2,2);
nexttile(tcl)
line1 = plot(1:10,rand(1,10),'b','DisplayName','Data Axes 1');
title('Axes 1');
nexttile(tcl)
line2 = plot(1:10,rand(1,10),'g','DisplayName','Data Axes 2');
title('Axes 2');
nexttile(tcl)
line3 = plot(1:10,rand(1,10),'r','DisplayName','Data Axes 3');
title('Axes 3');
nexttile(tcl)
line4 = plot(1:10,rand(1,10),'c','DisplayName','Data Axes 4');
title('Axes 4');
hL = legend([line1,line2,line3,line4]);
hL.Layout.Tile = 'East';
For for information on the 'Layout' property, refer to this page:
For MATLAB versions prior to MATLAB R2019b or code that uses 'subplot' instead of 'tiledlayout', there is no straight-forward way to create an overall legend. A workaround is to create an extra subplot, or an additional row or column, and use that space for the legend. Here is an example that uses a 2-by-2 grid of subplots with a third column reserved for the legend.
figure()
subplot(2,3,1)
line1 = plot(1:10,rand(1,10),'b','DisplayName','Data Axes 1');
title('Axes 1');
subplot(2,3,2)
line2 = plot(1:10,rand(1,10),'g','DisplayName','Data Axes 2');
title('Axes 2');
subplot(2,3,4)
line3 = plot(1:10,rand(1,10),'r','DisplayName','Data Axes 3');
title('Axes 3');
subplot(2,3,5)
line4 = plot(1:10,rand(1,10),'c','DisplayName','Data Axes 4');
title('Axes 4');
ax = subplot(2,3,3,'Visible','off');
axPos = ax.Position;
delete(ax)
hL = legend([line1,line2,line3,line4]);
hL.Position(1:2) = axPos(1:2);
You may use the 'Position' property of the legend to relocate it, for example, to adjust its vertical position.