Linkaxes, auto scale and debugger unusual behaviour
9 次查看(过去 30 天)
显示 更早的评论
I am using R2023a. When I run the code below, I get a different result if I set a breakpoint. If I run the code straight through, the y axis limits end up ad just within plus/minus 25. If I set a breakpoint at the linkaxes statement and then step through the code, the y axis limits end up at plus/minus 5.
Further, I am not sure how this is supposed to behave, but the behaviour I want is for all 3 plots to auto scale to whatever plot has the highest amplitudes. When I step through the code with the debugger, each of the auto scale lines at the end of the code rescales all 3 plots to a lower scale, so the first plot goes off the chart.
Code originally from here: https://au.mathworks.com/help/matlab/ref/linkaxes.html
h = tiledlayout(3, 1);
plotList = [];
% First plot
h1 = nexttile(h);
hold on
plotList = [plotList; h1];
x1 = linspace(0,6);
y1 = 8*sin(x1);
line1 = plot(h1, x1,y1);
% Second plot
h2 = nexttile(h);
hold on
plotList = [plotList; h2];
x2 = linspace(0,10);
y2 = 2*sin(2*x2);
plot(h2, x2,y2)
% Third plot
h3 = nexttile(h);
hold on
plotList = [plotList; h3];
x3 = linspace(0,12,200);
y3 = 4*sin(6*x3);
plot(h3, x3,y3)
linkaxes(plotList, 'xy')
ylim(h1, 'auto')
ylim(h2, 'auto')
ylim(h3, 'auto')
set(line1, 'YData', 3*y1)
0 个评论
回答(1 个)
Dinesh
2024-1-30
Hello.
The discrepancy in the y-axis limits you are observing as I understand it is likely because the "ylim('auto')" command is executed before the update to the "YData" property of "line1", which changes the range of data displayed. To resolve this, you may move the "set(line1, 'YData', 3*y1)" command to before the "ylim('auto')" commands. This ensures that when MATLAB calculates the automatic y-axis limits, it considers the updated data range.
The correct behavior which is having all plots auto-scale to the plot with the highest amplitude will be achieved by setting the "YData" first, then linking the axes with "linkaxes(plotList, 'xy')", and finally using "ylim('auto')" to auto-scale the y-axis according to the updated data. This way, running the code either with or without breakpoints will consistently give you the same result, with all three plots scaled appropriately.
Documentation for "ylim": https://www.mathworks.com/help/matlab/ref/ylim.html
3 个评论
Dinesh
2024-1-30
移动:Stephen23
2024-1-30
My understanding was incorrect. I thought your query was that the y-values printed by setting a breakpoint differ from what you see in the figure. Now that I read it, the figure itself seems to be different when you run the code directly versus using a debugger and then clicking "Continue" after the breakpoint is hit.
I could also reproduce the issue that you were facing. If I find a solution for this, I will update my answer accordingly.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!