0

i am simply printing there floats form my arduino or any other serial output device... in matlab i am receiving them and plotting them. it is being printed in arduino very fast but in matlab it is slower and after a a minute or so the plot is not reacting to the numbers from arduino fast... it seams matlab is slower and it is keeping the records in buffer? how can i clear it and get the fresh data plotted?

one more thing does it make sense that matlab can't import and plot a few numbers in 100Hz? What i am doing wrong or inefficient ?

clc 
clear all
h = figure(1);
set(h,'UserData',1);

s=serial('/dev/tty.usbmodem1411','BaudRate',115200);
set(s,'DataBits',8);
set(s,'StopBits',1);
fopen(s);
s.ReadAsyncMode='continuous';
readasync(s);
tStart = tic;
xplot=subplot(3,1,1);
hold on;
xlabel('time(ms)');
ylabel('X angle(deg)');
yplot=subplot(3,1,2);
hold on;
xlabel('time(ms)');
ylabel('Y angle(deg)');
zplot=subplot(3,1,3);
hold on;
xlabel('time(ms)');
ylabel('Z angle(deg)');

cont=true;
xAngle = zeros(1,1000000);
 yAngle = zeros(1,1000000);
zAngle = zeros(1,1000000);
i=0;
while(true)
i=i+1;
    t = toc(tStart);
%angle = fscanf(s, '%f');
[x y z] = strread(fgetl(s,s.BytesAvailable),'%f %f %f');
plot(xplot,t,x,'.');

plot(yplot,t,y,'.');

plot(zplot,t,z,'.');

drawnow;

end fclose(s);

alireza
  • 41
  • 2
  • 9

1 Answers1

0

It is likely that the speed of the 'plot' command is the problem. Creating a new plot is a slow operation, and so after you have received a few points, the time it takes to create the plot gets large. If you want to plot "real time" data in matlab, you will need to use some methods such as discussed in this question.

You can investigate the situation by changing your loop code to read data from the device and just print the line on the screen without plotting it. I expect you will see that the numbers can be printed as fast as they are received.

Community
  • 1
  • 1
mhopeng
  • 1,063
  • 1
  • 7
  • 17