如何替换 Matlab 图形中的 x 轴?
How to replace x-axis in a Matlab figure?
我需要什么命令才能在不影响 y 轴值的情况下移动打开的 Matlab 图形中的 x 轴值? (如下图所示)
到目前为止我最好的猜测是:
LineH = get(gca, 'Children');
x = get(LineH, 'XData');
y = get(LineH, 'YData');
offset=20;
nx = numel(x);
for i=1:nx
x_shifted{i} = x{i} + offset;
end
set(LineH,'XData',x_shifted')
这给了我错误:
Error using matlab.graphics.chart.primitive.Line/set
While setting the 'XData' property of Line:
Value must be a vector of numeric type
谢谢!
显然您不能使用元胞数组同时设置所有行的 'XData'
属性。
EDIT 可以做到;见 .
你可以做的只是将 set
语句移动到循环中:
LineH = get(gca, 'Children');
x = get(LineH, 'XData');
y = get(LineH, 'YData');
offset=20;
nx = numel(x);
for i=1:nx
x_shifted{i} = x{i} + offset;
set(LineH(i),'XData',x_shifted{i}); % set statement with index i
end
您必须封装 'XData'
property name in a cell to update multiple plot objects at a time. From the set
文档:
set(H,NameArray,ValueArray)
specifies multiple property values using the cell arrays NameArray
and ValueArray
. To set n
property values on each of m
graphics objects, specify ValueArray
as an m
-by-n
cell array, where m = length(H)
and n
is equal to the number of property names contained in NameArray
.
因此,要修复您的错误,您只需将最后一行更改为:
set(LineH, {'XData'}, x_shifted');
如果您有兴趣,这里有一个使用 cellfun
而不是循环的解决方案:
hLines = get(gca, 'Children');
xData = get(hLines, 'XData');
offset = 20;
set(hLines, {'XData'}, cellfun(@(c) {c+offset}, xData));
我需要什么命令才能在不影响 y 轴值的情况下移动打开的 Matlab 图形中的 x 轴值? (如下图所示)
到目前为止我最好的猜测是:
LineH = get(gca, 'Children');
x = get(LineH, 'XData');
y = get(LineH, 'YData');
offset=20;
nx = numel(x);
for i=1:nx
x_shifted{i} = x{i} + offset;
end
set(LineH,'XData',x_shifted')
这给了我错误:
Error using matlab.graphics.chart.primitive.Line/set
While setting the 'XData' property of Line:
Value must be a vector of numeric type
谢谢!
显然您不能使用元胞数组同时设置所有行的 'XData'
属性。
EDIT 可以做到;见
你可以做的只是将 set
语句移动到循环中:
LineH = get(gca, 'Children');
x = get(LineH, 'XData');
y = get(LineH, 'YData');
offset=20;
nx = numel(x);
for i=1:nx
x_shifted{i} = x{i} + offset;
set(LineH(i),'XData',x_shifted{i}); % set statement with index i
end
您必须封装 'XData'
property name in a cell to update multiple plot objects at a time. From the set
文档:
set(H,NameArray,ValueArray)
specifies multiple property values using the cell arraysNameArray
andValueArray
. To setn
property values on each ofm
graphics objects, specifyValueArray
as anm
-by-n
cell array, wherem = length(H)
andn
is equal to the number of property names contained inNameArray
.
因此,要修复您的错误,您只需将最后一行更改为:
set(LineH, {'XData'}, x_shifted');
如果您有兴趣,这里有一个使用 cellfun
而不是循环的解决方案:
hLines = get(gca, 'Children');
xData = get(hLines, 'XData');
offset = 20;
set(hLines, {'XData'}, cellfun(@(c) {c+offset}, xData));