MATLAB: Changing YLabel position and outerposition

activepositionpropertyaxesaxisinnerpositionMATLABouterpositionouterposition labelposition;positionconstraintrotated label

If YLabel position change that outerposition mode of axes don't work for YLabel.
It's correct?
figure;
ax1 = axes('OuterPosition',[0 0.50 1.0 0.50]);
ax1.ActivePositionProperty = 'outerposition';
plot(ax1,0:10,0:10);
ax1.Title.String = 'Preserve OuterPosition';
ax1.YLabel.Rotation = 0;
ax1.YLabel.String = 'Preserve OuterPosition';
ax1.YLabel.HorizontalAlignment = 'right';
ax1.YLabel.VerticalAlignment = 'top';
ax1.YLabel.Position(2) = ax1.YLabel.Position(2)+2; % outerposition mode of axes don't work for YLabel after the line
ax1.YLabel.HorizontalAlignment = 'right';
ax1.YLabel.VerticalAlignment = 'top';
outerpos = ax1.OuterPosition;
ti = ax1.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax1.Position = [left bottom ax_width ax_height];

Best Answer

Source of the problem (scroll down for solution)
TL;DR: Changing the position of the y-axis label triggers an axis resize that doesn't account for rotated y-axis labels.
Here's a demo similar to yours that illustrates the problem.
1) Create an axes with a y-axis label in its default orientation. Draw a red rectangle around the OuterPosition of the axes and a green rectangle around the Position/InnerPosition of the axes.
figure('color', 'w', 'MenuBar', 'none');
ax1 = axes('OuterPosition',[.25 0.40 .7 0.50]);
ax1.ActivePositionProperty = 'outerposition';
plot(ax1,0:10,0:10);
ax1.Title.String = 'Preserve OuterPosition';
ax1.YLabel.String = 'Preserve OuterPosition';
ah(1) = annotation('rectangle',ax1.OuterPosition,'Color', 'r','LineWidth',2);
ah(2) = annotation('rectangle',ax1.Position,'Color','g','LineWidth',2);
2) Rotate the y-axis label and set alignment. Draw blue dashed rectangles showing the updated position properties.
ax1.YLabel.Rotation = 0;
ax1.YLabel.HorizontalAlignment = 'right';
ax1.YLabel.VerticalAlignment = 'top';
ah(3) = annotation('rectangle',ax1.OuterPosition, 'Color', 'b', 'LineStyle','--','LineWidth',2);
ah(4) = annotation('rectangle',ax1.Position, 'Color', 'b','lineStyle','--','LineWidth',2);
As you can see, the OuterPosition propery is preserved and the Position/InnerPosition properties have been adapted to the rotated y-axis label.
3) Change the vertical position of the y-axis label. Draw black dotted rectangles showing the updated position properties.
ax1.YLabel.Position(2) = ax1.YLabel.Position(2)+2; % outerposition mode of axes don't work for YLabel after the line

ah(5) = annotation('rectangle',ax1.OuterPosition, 'Color', 'k', 'LineStyle',':','LineWidth',3);
ah(6) = annotation('rectangle',ax1.Position, 'Color', 'k','lineStyle',':','LineWidth',3);
As you can see, the original position property values have been returned as if the y-axis label were still oriented at 90 degrees.
Solution to the problem
Record the Position value of the axes prior to changing the y-axis label position. After changing the y-axis label position, reset the axes to its original position.
This is applied to the code from your demo.
figure;
ax1 = axes('OuterPosition',[0 0.50 1.0 0.50]);
plot(ax1,0:10,0:10);
ax1.Title.String = 'Preserve OuterPosition';
ax1.YLabel.String = 'Preserve OuterPosition';
ax1.YLabel.Rotation = 0;
ax1.YLabel.HorizontalAlignment = 'right';
ax1.YLabel.VerticalAlignment = 'top';
prePosition = ax1.Position; % RECORD THE POSITION OF THE AXES PRIOR TO LABEL POSITION CHANGE
ax1.YLabel.Position(2) = ax1.YLabel.Position(2)+2; % outerposition mode of axes don't work for YLabel after the line
ax1.Position = prePosition; % RESET THE AXIS POSITION