Matrix data plot

3 次查看(过去 30 天)
Gustl
Gustl 2012-1-20
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!

采纳的回答

the cyclist
the cyclist 2012-1-20
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
  1 个评论
Gustl
Gustl 2012-1-20
Cyclist, thank you very much for your help ! Works like a charm!

请先登录,再进行评论。

更多回答(3 个)

the cyclist
the cyclist 2012-1-20
Does this do what you mean?
x1 = rand(8,1);
x2 = rand(8,1);
y1 = rand(8,1);
y2 = rand(8,1);
plot([x1 x2]',[y1 y2]')

Gustl
Gustl 2012-1-20
Thank you very much for the answer. Yes, this does exactly what I want to do. But the problem is, if I want to make the plot faster by setting the data to the plot handle, it reports "Value must be a column or row vector"..
So this is my code:
figure
h1=plot(0,0,'r-');hold on;
x1 = rand(8,1);
x2 = rand(8,1);
y1 = rand(8,1);
y2 = rand(8,1);
plot([x1 x2]',[y1 y2]'); %this does what i want to do
set(h1,'xdata',[x1 x2]','ydata',[y1 y2]') % this doens't work
Any ideas?

Gustl
Gustl 2012-1-20
I've also tried by assigning the variable to the properties "xdatasource" and "ydatasource" by using
h1=plot(0,0,'r-');
x1 = rand(8,1);
x2 = rand(8,1);
y1 = rand(8,1);
y2 = rand(8,1);
xx=[x1 x2]';yy=[y1 y2]';
set(h1,'xdatasource','xx','ydatasource','yy');
refreshdata;
...but still without success...Is it somehow possible to get the this work?
  1 个评论
Sean de Wolski
Sean de Wolski 2012-1-20
h = plot([x1 x2]',[y1 y2]')
You will see that h is a vector with a handle to each line data point. You could manually set the xdata/ydata for each line, this would likely be fast.
I would use a for-loop to generate n lines, each with it's own xdata/ydata.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Spline Postprocessing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by