我正在开展一个项目,其中包括使用加速度计和陀螺仪来获取物体的方向。我可以通过串行通信将3d方向向量从arduino传递给matlab。
我想在matlab中绘制矢量来进行实时分析。我在循环中使用quiver3d
和drawnow
函数绘制向量,但quiver3d
函数非常慢,所以我可以在20秒之后看到对象的方向。
有没有办法更快地绘制3d矢量?
感谢。
答案 0 :(得分:4)
quiver
图可能太多,无法在3-D中绘制一个矢量。您可以使用简单的plot3
(如下图所示)来实现类似的绘图。
在此图中,矢量的原点是蓝点,方向由红线给出。
代码
%v is the direction of the vector (3 cartesian coordinates)
v = sort(randn(100,3));
v = bsxfun(@rdivide,v,sqrt(sum(v.^2,2)));
%xyz the origin of the vector
ind = linspace(-pi,pi,100);
x = cos(ind);
y = sin(ind);
z = ind;
%the plotting function
figure
for ii = 1:numel(ind)
plot3(x(ii),y(ii),z(ii),'bo'); %origin in blue
set(gca,'XLim', [-3 3], 'YLim', [-3 3], 'ZLim', [-3 3]);
hold on;
hl = plot3( linspace(x(ii), x(ii)+v(ii,1),10), ...
linspace(y(ii), y(ii)+v(ii,2),10), ...
linspace(z(ii), z(ii)+v(ii,3),10), ...
'r'); %direction in red
view(80,10);
pause(0.1);
%clf
end