0
votes

I have two task to do

  1. getting data serially from microcontroller.
  2. plotting the 3 axis value in real time.

For first I used the following code:

s=serial('COM10');
fopen(s);
out=fscanf(s);
while(out~=0)
out=fscanf(s);
disp(out);
end
fclose(s);

now in second part i have to plot there data in real time how can i do it ,m new to matlab i tried the following sample code to plot 3 values but didn't worked out. please help.

x = -50;
y = 10;
z = 20;
while(1)

plot3(x,y,z);
XLABEL('X Axis');
YLABEL('Y Axis');
ZLABEL('Z Axis');
set(gca, 'XColor', 'r', 'YColor', [0 0.5 0.5], 'ZColor', 'y')
x=x+2;
y=y+2;
z=z+2;
end
1
What didn't work about the solution you posted? Please, use capitals in your text. - Bernhard
@Bernhard The plot isn't showing any lines ,as idea was to plot 3 values in a infinite loop.So i can implement the same when i will we getting the 3 axis value over uart continiously.Is this the right approach? - pranjal khanduri

1 Answers

0
votes

Change the code, use plot only once:

X=[];Y=[];Z=[];
x=0;y=0;z=0;
figure(1);
myplot=plot3(x,y,z);
while(1)
    x=x+1;
    y=sin(x);
    z=cos(x);
    X(end+1)=x;
    Y(end+1)=y;
    Z(end+1)=z;
    set(myplot,'Xdata',X,'YData',Y,'ZData',Z);
    drawnow limitrate;
end

If the loop run long enough, don't forget to limit X,Y,Z sizes(for example every 1000 samples delete 500)