get axis limits actually used after assigning [-inf inf -inf inf]
2 次查看(过去 30 天)
显示 更早的评论
My script is written to use either [-inf inf -inf inf] or an assigned set of axis limits as a variable since the script may instead compute a desired set of limits to be applied as the limit set. I need to know the limits that are actually used in order to add text to the graph. If I assign specific limits to the axis as a variable (computed or manually assigned), no problem. But if [-inf inf -inf inf] is used as the variable, any inquiry as to the actual limits used < get(gca,'ylim') > after plotting and before adding the text, the values returned are [-inf inf], not the actual values. How do I get the values actually used when [-inf inf -inf inf] is the default input?
3 个评论
回答(1 个)
Steven Lord
2016-12-13
If you want to let MATLAB automatically determine the limits, don't set the limits to plus or minus infinity. Instead set the axes property XLimMode to 'auto'. Once MATLAB has computed the limits, get the limits via the XLim axes property or the xlim function.
3 个评论
Stephen23
2016-12-14
编辑:Stephen23
2016-12-14
@Tim: what you are doing is very indirect, and now you are finding out that it has disadvantages too. Good program design does not mean "use lots of code shortcuts". Doing so will not make your code more reliable nor make it easier to write, and it certainly will not make it easier to debug. Good code practices do include using features to do the things that they were designed to do.
If you want auto axis limits then set XLimMode: this is what it is designed for.
Guillaume
2016-12-14
编辑:Guillaume
2016-12-14
"If you want auto axis limits then set XLimMode: this is what it is designed for."
...and with the current design is trivial to implement anyway:
%... some code
hax = gca; %should actually be obtained when the axis is created. Using gca is dangerous
if all(isinf(mylimits))
hax.XLimMode = 'auto';
else
hax.Xlim = mylimits;
end
%...
However, I do agree that there should be a way to retrieve the actual axis limits when one or both are set to +/-Inf. XLimMode 'auto' is not going to work when you only want one of the limit on auto.
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!