I have a problem with plotting the data read from the serial port from an Arduino uno board.

8 views (last 30 days)
Hi Everyone, I am trying to read the data printed from an Arduino board with MATLAB serial read and I have problem when I plot it real time. After some time, the data becomes delayed and it has lagged response to my input. Actually, there is a motor and its current is being read. Please take a look at my code and see if you can advise on why this happens. Also, I would like to know how to plot the data versus time. Thank you. Sina.
delete(instrfind({'Port'},{'COM4'}));
clear all;
s1=serial('COM4','Baudrate',115200);
set(s1,'DataBits',8);
set(s1,'StopBits',1);
set(s1,'Parity','none');
fopen(s1);
x=[0];
SS=[1];
str='';
j=1;
tic;
time=0;
while(j<10000);
str=fgetl(s1);
ss=str2num(str);
while isempty(ss);
sss=fgetl(s1);
ss=str2num(sss);
end
if j<=1;
SS(j)=ss;
x(1)=1;
elseif j>1;
SS(j)=mean([SS(j-1);ss]);
x(j)=toc;
end
if isreal(ss);
% ts = timeseries(SS(1:j));
if (exist('last'))
plot([x(j),get(last,'xdata')],[SS(j),get(last,'ydata')]);
end
last=plot(x(j), SS(j), '.-');
% plot(time, SS(j)/1000, '.-')
hold on
% siz=size(x);
% plot(x(j), siz(2), '.-r')
axis([toc-10 toc+10 0 4000]);
end
drawnow;
hold on
grid on
j=j+1;
pause(0.00001);
end
ElapsedTime=toc;
delete(instrfind({'Port'},{'COM4'}));
% fclose(s)
% delete(s)
clear s1

Accepted Answer

Geoff Hayes
Geoff Hayes on 8 Aug 2014
Sina - you say that After some time, the data becomes delayed and it has lagged response to my input. What exactly of you mean by this? What is your input? Does the MATLAB code take longer to read from the serial device and so the code is spending more time in the
while isempty(ss);
sss=fgetl(s1);
ss=str2num(sss);
end
loop? Or is it that the code is doing more work and is not responding fast enough to your inputs?
The following seems to be doing more than it should or need
if (exist('last'))
plot([x(j),get(last,'xdata')],[SS(j),get(last,'ydata')]);
end
last=plot(x(j), SS(j), '.-');
The above is putting the most recent element at the beginning of the array, and then re-plotting all of the data. Only to re-plot the latest element again. Are you plotting time from right to left? And isn't x(1:j-1) equal to get(last,'data')? Are you doing this because the new plot command erases all previous plots, and you are trying to show past history on the figure? If so, then consider using the hold on command. I see that you are using this command already, but I don't think you need both it and the code to re-plot all previous data. You may be able to replace the above with just
plot(x(j),SS(j),'.-');
if j==1
hold on;
end
As well, consider pre-allocating memory to your x and SS arrays so that they do not grow at each iteration. Since j is at most 10000, then
numIterations = 10000;
x = zeros(numIterations,1);
SS = ones(numIterations,1);
% etc.
while j<numIterations
% do stuff
end
Also, you may want a pause in the first inner while loop
while isempty(ss);
sss=fgetl(s1);
ss=str2num(sss);
pause(0.01);
end
just so that the CPU is able to process other threads.
  6 Comments
Sina
Sina on 18 Sep 2014
Sorry Geoff, I was away for some time. If I don't plot it, I won't know what happens, the motor turns indefinitely.
jawad mansoor
jawad mansoor on 29 Jul 2016
Edited: jawad mansoor on 29 Jul 2016
Hey, I solved this problem like this.
plot(object handle,data...);
hold on;
while(1)
gca;
%%acquire new data
%%acquire new data
plot(object handle,data...);
drawnow;
end
gca clears data on your axis, and with hold on you plot on the same axis. it is much faster. gives almost real time graphs for me.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!