MATLAB: Matrix data plot

matrix data plotmultiline plot

I have 4 vectors, x1,y1,x2,y2. where x1 and y1 represent starting points and x2 and y2 are end points of lines, which I want to plot. Each i-th pair [x1(i), y1(i) x2(i) y2(i)] represents new separated line.
I would like to execute the plotting with a single plot command. I plot my data with set(h1,'xdata',…,'ydata',…) command for speed optimization. Is it possible to execute the plotting with a single plot command ?
I appreciate your help!

Best Answer

The best way is to use x and y as data sources. I don't have much experience with this, but here is a working example. I tried to initialize with (0,0) or empty sets for x and y, but that choked for reasons that I do not understand. Maybe this will get you started, though.
x1 = rand(8,1);
x2 = rand(8,1);
x = [x1 x2]';
y1 = rand(8,1);
y2 = rand(8,1);
y = [y1 y2]';
figure
h = plot(x,y,'XDataSource','x','YDataSource','y');
for np = 1:10,
x1 = rand(8,1);
x2 = rand(8,1);
x = [x1 x2]';
y1 = rand(8,1);
y2 = rand(8,1);
y = [y1 y2]';
refreshdata(h,'caller')
drawnow
pause(0.5)
end
Related Question