get function returns unexpected values

I have a function, which creates a figure, plots something and changes several plot parameters. At some point I want to query current values of y-tick labels, which I do via:
current_ticks = get(gca, 'YTick');
The values that I get here are different from what I see on the plot once it's displayed. I believe this happens because get tries to query the plot in an intermediate state, before all adjustsments are processed, since putting
pause(0.1)
(or simply pausing in the debugger) right before the call to get changes its answer and makes it consistent with what I see in the figure. The same things happens if I use yticks function. Is there a better solution to this?

댓글 수: 4

Geoff Hayes
Geoff Hayes 2020년 8월 20일
Igor - can you provide a minimal working example that demonstrates this behaviour?
Well, the setting of tick-marks and such varies in a way (that I guess) is pleasing to the viewer. If you for example change the size of the figure you might get a larger number of tick-marks. This I've assumed is a deliberate design-choise of Mathworks. For your functionality it might be difficult to get a much better and robust solution. However it might look neater (and save you 0.1 s per run!) if you use drawnow instead of pause(0.1).
Igor Gayday
Igor Gayday 2020년 8월 20일
편집: Igor Gayday 2020년 8월 20일
Geoff, here is an example:
f = figure;
f.WindowState = 'maximized';
axis = gca;
axis.FontSize = 20;
hold on
plot(1:10, 1:10);
ylim([0, 100]);
y_ticks = get(gca, 'YTick');
As Bjorn correctly pointed out, the issue stems from the change of window's size. If you run this code, the values of y_ticks will be the same as if the window was not maximized. In the maximized version, displayed to me after the execution of this code, the ticks will be different.
Igor Gayday
Igor Gayday 2020년 8월 20일
Bjorn, thank you, drawnow fixes this.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2020년 8월 20일

댓글:

2020년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by