The 'YDir'轴属性 https://www.mathworks.com/help/matlab/ref/axes-properties.html#property_XDir可以是'normal'
or 'reverse'
。默认情况下是'normal'
对于大多数图,但有些图会自动将其更改为'reverse'
, 如那个image https://www.mathworks.com/help/matlab/ref/image.html or imagesc https://www.mathworks.com/help/matlab/ref/imagesc.html功能。
您可以使用以下任一方式设置轴的 y 轴方向set https://www.mathworks.com/help/matlab/ref/set.html函数或点索引(在较新的 MATLAB 版本中):
h = gca; % Handle to currently active axes
set(h, 'YDir', 'reverse');
% or...
h.YDir = 'reverse';
我对其他一些答案感到困惑'YDir'
财产不知何故消失或出现错误。我在 2013 年、2014 年或 2016 年的 MATLAB 版本中没有看到任何此类行为。我只遇到了两个潜在的陷阱:
-
该属性不能使用元胞数组设置,只能使用字符串:
>> set(gca, 'YDir', {'reverse'});
Error using matlab.graphics.axis.Axes/set
While setting property 'YDir' of class 'Axes':
Invalid enum value. Use one of these values: 'normal' | 'reverse'.
虽然这有效:
set(gca, {'YDir'}, {'reverse'}); % Property name is also a cell array
-
The gca https://www.mathworks.com/help/matlab/ref/gca.html执行点索引时,函数不能互换用作句柄(这就是为什么我首先将其保存到变量中h
在上面的例子中):
>> gca.YDir
Undefined variable "gca" or class "gca.YDir".
>> gca.YDir = 'reverse' % Creates a variable that shadows the gca function
gca =
struct with fields:
YDir: 'reverse'
最后,如果您想要一些可以切换的代码'YDir'
属性无论其当前状态如何,您都可以执行以下操作:
set(gca, 'YDir', char(setdiff({'normal', 'reverse'}, get(gca, 'YDir'))));
% or...
h = gca;
h.YDir = char(setdiff({'normal', 'reverse'}, h.YDir));