MATLAB: How to plot 3 different lines in single 3 d plot

plotting 3 axis

i have accelerometer which gives 3 axis values.i read the data from accelerometer and plotting a graph. i succeeded in plotting only 1-axes value and the code is as shown below
[gx,gy,gz] = readAcc(accelerometer);
gxdata=[gxdata(2:end); gx];
plot(index,gxdata,'r');
axis([1 buf_len -3.5 3.5]);
i want to plot all gx,gy,gz values in a single 3D plot with different color lines.how to do that?

Best Answer

If you have R2014b or later, I suggest you create some animatedLine with your desired maximum length, and addpoint() the data as it comes in.
If you are not trying to limit the length of the lines then I would suggest:
gdata = []; %initialize

for index = 1 : inf
[gx, gy, gz] = readAcc(accelerometer);
gdata = [gdata; gx, gy, gz];
plot(1:index, gdata);
drawnow();
end
Except that more efficient would be:
gdata = []; %initialize
hx = line(nan, nan, 'color', 'r'); %do not use [] to initialize them
hy = line(nan, nan, 'color', 'g');
hz = line(nan, nan, 'color', 'b');
for index = 1 : inf
[gx, gy, gz] = readAcc(accelerometer);
gdata = [gdata; index, gx, gy, gz];
set(hx, 'XData', gdata(:,1), 'YData', gdata(:,2));
set(hy, 'XData', gdata(:,1), 'YData', gdata(:,3));
set(hz, 'XData', gdata(:,1), 'YData', gdata(:,4));
drawnow();
end
Related Question