Different result with Run (F5) and line by line debugging (F10)

3 views (last 30 days)
Joshua
Joshua on 2 Dec 2021
Commented: Joshua on 2 Dec 2021
Considering the following minimum working example:
figure()
ax1 = subplot(2,1,1); hold on;
plot([0 1],[0 0],'DisplayName','Line 1')
plot([0 1],[1 1],'DisplayName','Line 2')
plot([0 1],[2 2],'DisplayName','Line 3')
plot([0 1],[3 3],'DisplayName','Line 4')
legend('show','Location','Northoutside')
ax1Pos = ax1.Position
ax2 = subplot(2,1,2); hold on;
plot([0 1],[0 0],'DisplayName','Line 1')
plot([0 1],[1 1],'DisplayName','Line 2')
plot([0 1],[2 2],'DisplayName','Line 3')
plot([0 1],[3 3],'DisplayName','Line 4')
I observe a difference in the value of ax1Pos depending on wether I run the script (F5) or step through line by line (F10). For the first case I get the position of the subplot axis INCLUDING the legend, in the second case I get the position of the subplot axis EXCLUDING the legend. Any ideas why that is the case?
Output:
>> test
ax1Pos =
0.130000000000000 0.583837209302326 0.775000000000000 0.341162790697675
>> test
8 ax1Pos = ax1.Position
ax1Pos =
0.130000000000000 0.583837209302326 0.775000000000000 0.155930332361357
I am using MATLAB R2021b.

Accepted Answer

Benjamin
Benjamin on 2 Dec 2021
Someone more knowledgeable than me may know for sure, but it may be because the event queue gets processed each time you dbstep, whereas the event queue doesn't get processed until the end when you just run the code. If you put a drawnow() immediately before the line "ax1Pos = ax1.Position", I believe you should see the same position in both cases (at least this is what I observe in R2017b).

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by