1

This is a continuation from the question already posted here. I used the method that @Andrey suggested. But there seems to be a limitation. the set(handle, 'XData', x) command seems to work as long as x is a vector. what if x is a matrix?

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). The command that will be used to plot is simply plot(x,y).

Now, we want to update the above plot. This time we want to draw 4 rectangles. whose vertices are present in the matrices x_new(5,4 matrix) and y_new (5,4 matrix) that we obtain after some calculations. Now using the command set(handle, 'XData', x, 'YData', y) after updating x and y with new values results in an error that states

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
Community
  • 1
  • 1
bvk
  • 75
  • 7

1 Answers1

1

Each line on the plot has its own XData and YData properties, and each can be set to a vector individually. See the reference. I am not at a Matlab console right now, but as I recall...

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.

cxw
  • 16,685
  • 2
  • 45
  • 81
  • my first look at the code tells me that you are modifying existing lines. But for me, the dimension of `x_new` and `y_new` will change every time I run your for loop. In other words, the number of lines that I want to draw are not constant. How do I add newlines or remove existing lines from the plot? correct me if I am wrong somewhere. – bvk Dec 09 '14 at 02:22
  • @bvk - edited the answer to indicate. Again, doing this from memory, so YMMV :) – cxw Dec 09 '14 at 02:53