[英]How should I update the data of a plot in Matlab? part - 2
This is a continuation from the question already posted here . 这是已经在此处发布的问题的继续。 I used the method that @Andrey suggested. 我使用了@Andrey建议的方法。 But there seems to be a limitation. 但是似乎有一个限制。 the set(handle, 'XData', x)
command seems to work as long as x is a vector. 只要x是向量, set(handle, 'XData', x)
命令似乎就可以工作。 what if x is a matrix? 如果x是矩阵怎么办?
Let me explain with an example. 让我举例说明。 Say we want to draw 3 rectangles whose vertices are given by the matrices x_vals
(5,3 matrix) and y_vals
(5,3 matrix). 假设我们要绘制3个矩形,其顶点由矩阵x_vals
(5,3矩阵)和y_vals
(5,3矩阵)给出。 The command that will be used to plot is simply plot(x,y)
. 用于绘制的命令只是plot(x,y)
。
Now, we want to update the above plot. 现在,我们要更新上面的图。 This time we want to draw 4 rectangles. 这次我们要绘制4个矩形。 whose vertices are present in the matrices x_new
(5,4 matrix) and y_new
(5,4 matrix) that we obtain after some calculations. 其顶点存在于我们经过一些计算获得的矩阵x_new
(5,4矩阵)和y_new
(5,4矩阵)中。 Now using the command set(handle, 'XData', x, 'YData', y)
after updating x
and y
with new values results in an error that states 现在在使用新值更新x
和y
之后使用命令set(handle, 'XData', x, 'YData', y)
导致错误,指出
Error using set Value must be a column or row vector
Any way to solve this problem? 有什么办法解决这个问题?
function [] = visualizeXYZ_struct_v3(super_struct, start_frame, end_frame)
% create first instance
no_objs = length(super_struct(1).result);
x = zeros(1,3000);
y = zeros(1,3000);
box_x = zeros(5, no_objs);
box_y = zeros(5, no_objs);
fp = 1;
% cascade values across structures in a frame so it can be plot at once;
for i = 1:1:no_objs
XYZ = super_struct(1).result(i).point_xyz;
[r,~] = size(XYZ);
x(fp:fp+r-1) = XYZ(:,1);
y(fp:fp+r-1) = XYZ(:,2);
% z(fp:fp+r-1) = xyz):,3);
fp = fp + r;
c = super_struct(1).result(i).box;
box_x(:,i) = c(:,1);
box_y(:,i) = c(:,2);
end
x(fp:end) = [];
y(fp:end) = [];
fig = figure('position', [50 50 1280 720]);
hScatter = scatter(x,y,1);
hold all
hPlot = plot(box_x,box_y,'r');
axis([-10000, 10000, -10000, 10000])
xlabel('X axis');
ylabel('Y axis');
hold off
grid off
title('Filtered Frame');
tic
for num = start_frame:1:end_frame
no_objs = length(super_struct(num).result);
x = zeros(1,3000);
y = zeros(1,3000);
box_x = zeros(5, no_objs);
box_y = zeros(5, no_objs);
fp = 1;
% cascade values accross structures in a frame so it can be plot at once;
for i = 1:1:no_objs
XYZ = super_struct(num).result(i).point_xyz;
[r,~] = size(XYZ);
x(fp:fp+r-1) = XYZ(:,1);
y(fp:fp+r-1) = XYZ(:,2);
fp = fp + r;
c = super_struct(num).result(i).box;
box_x(:,i) = c(:,1);
box_y(:,i) = c(:,2);
end
x(fp:end) = [];
y(fp:end) = [];
set(hScatter, 'XData', x, 'YData', y);
set(hPlot, 'XData', box_x, 'YData', box_y); % This is where the error occurs
end
toc
end
Each line on the plot has its own XData
and YData
properties, and each can be set to a vector individually. 绘图上的每条线都有其自己的XData
和YData
属性,并且可以分别设置为向量。 See the reference . 请参阅参考资料 。 I am not at a Matlab console right now, but as I recall... 我现在不在Matlab控制台上,但是我记得...
kidnum = 1
h_axis = gca % current axis - lines are children of the axis
kids = get(h_axis,'Children')
for kid = kids
kid_type = get(kid,'type')
if kid_type == 'line'
set(kid,'XData',x_new(:,kidnum))
set(kid,'YData',y_new(:,kidnum))
kidnum = kidnum+1
end
end
Hope that helps! 希望有帮助! See also the overall reference to graphics objects and properties. 另请参见有关图形对象和属性的整体参考 。
To add a series, say 要添加系列,例如
hold on % so each "plot" won't touch the lines that are already there
plot(x_new(:,end), y_new(:,end)) % or whatever parameters you want to plot
After that, the new series will be a child of h_axis
and can be modified. 之后,新系列将成为h_axis
的子级,并且可以进行修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.