我应该如何在Matlab中更新绘图数据?第2部分

时间:2014-12-08 20:03:45

标签: matlab matlab-figure

这是已发布的here问题的延续。我使用了@Andrey建议的方法。但似乎有一个限制。只要x是向量,set(handle, 'XData', x)命令似乎就可以工作。如果x是矩阵怎么办?

让我举个例子来解释一下。 假设我们要绘制3个矩形,其顶点由矩阵x_vals(5,3矩阵)和y_vals(5,3矩阵)给出。用于绘图的命令只是plot(x,y)

现在,我们想更新上面的情节。这次我们要绘制4个矩形。其顶点存在于矩阵x_new(5,4矩阵)和y_new(5,4矩阵)中,我们在一些计算后得到它们。现在,在使用新值更新set(handle, 'XData', x, 'YData', y)x后使用命令y会导致出现错误状态

Error using set Value must be a column or row vector

有什么方法可以解决这个问题吗?

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

1 个答案:

答案 0 :(得分:1)

图中的每一行都有自己的XDataYData属性,每个属性都可以单独设置为向量。见the reference。我现在不在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

希望有所帮助!另请参阅overall reference到图形对象和属性。

要添加一个系列,请说

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

之后,新系列将成为h_axis的孩子,可以修改。