Problem with set(gca, 'Position', [...]);
114 views (last 30 days)
Show older comments
Andrew Yool
on 21 Aug 2020
Commented: Walter Roberson
on 22 Aug 2020
Hi there,
I've recently moved from v2013a Matlab to v2020a (an overdue change driven by my institute). I've got a plotting script that I was working on at v2013a just the other day but which now behaves strangely at v2020a.
It has two subplots, and right at the end, repositions the second to match the first's horizonal position.
figure(1); clf;
subplot (2,1,1);
pcolor (tval, yval, t1); shading flat;
ylabel (ystr); title (titstr1);
cb = colorbar; pos1 = get(gca, 'Position');
subplot (2,1,2);
plot (tval, t2, 'k-');
ylabel (ystr);
xlabel ('Time [year]'); title (titstr2);
pos2 = get(gca, 'Position'); pos2(1) = pos1(1); pos2(3) = pos1(3); set (gca, 'Position', pos2);
What should happen is that the second subplot's horizontal length is shortened to match the first subplot's reduced length (which stems from it having a colorbar). I do this so that the two time-series are aligned on the plot.
And this script works fine at v2013a. But at v2020a, when I cut and paste the above into the command window (my preferred mode of working), it does everything except for the repositioning. Even though the command is right there. Even more strangely, if I then manually paste the final statement ...
set (gca, 'Position', pos2);
... into the command window after it's failed to do what I ask, it then does it correctly. I can think of no good reason why this is, and my attempts to "trick" Matlab (e.g. by duplicating the set statement, or by assigning a named handle to the subplot) have all failed. It needs me to separately paste in the final command for it to properly execute it. Which is completely impractical for this particular script because the plotting is meant to be part of a loop over many plots.
Over the (many) years I've used Matlab, I'm familiar with the ordering of commands being important to the final result, but this failure to do what it's told, and then doing what it's told (but forcing me to ask it in a clunky manual fashion) is new to me.
What am I missing about how v2020a works?
Best regards,
Andrew.
0 Comments
Accepted Answer
Walter Roberson
on 21 Aug 2020
Axes position is not calculated until the figure is rendered. You should add a drawnow() before getting the axes position.
2 Comments
Walter Roberson
on 22 Aug 2020
As of R2014b, graphics rendering was moved to a different thread, and the information about updates to the data structures is not sent to the graphics thread until drawnow() or pause() or a small number of other activities, including returning to the command line.
Especially when you use the new object notation to set properties, you really do not want to submit everything to rendering immediately. For example it is not uncommon to change the number of entries in xticks and then to update xticklabels, and with the new notation like
ax.XTicks = something
ax.XTickLabels = appropriate
then there is no explicit way to say "postpone updating the graphics, I am making a block of changes during which some of the data properties might be temporarily inconsistent... okay now it is safe to update again". The postpone becomes implicit, properties are permitted to be inconsistent until dranow() or one of the other specific activities takes place.
More Answers (0)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!