Thread Subject: Plotting a real time graph

Subject: Plotting a real time graph

From: arindam

Date: 28 Nov, 2008 11:02:37

Message: 1 of 12

I am designing a device, which needs to communicate with MATLAB via serial port

I need to have a CLI for taking values from the user which can be transmitted to the device. This has been done.

I have now run into this problem...
The device transmits a series of numbers which need to be plotted real time. Can this be done on MATLAB?
The CLI also needs to be active and available at the same time.

Could someone please point me in the right direction? Please feel free to offer any suggestions or ideas that you might have.

I am new to MATLAB, but not to programming.

Subject: Plotting a real time graph

From: arindam

Date: 28 Nov, 2008 12:59:40

Message: 2 of 12

I am not asking for a complete solution here ... I just a nudge in the right direction. You might mention any command or library, and i will dig into it.

Subject: Plotting a real time graph

From: Oliver Woodford

Date: 28 Nov, 2008 13:23:02

Message: 3 of 12

In terms of plotting you will find the following functions useful: plot, hold, drawnow.

For example:
>> figure;
>> hold on;
>> for a = 1:1e3
>> plot(rand(1), rand(1), 'r+');
>> drawnow;
>> end

I don't know about serial data, but perhaps you could try setting up a callback function to call the relevant plotting functions.

Subject: Plotting a real time graph

From: Ryan Ollos

Date: 28 Nov, 2008 13:48:02

Message: 4 of 12

arindam <goswamiarindam@gmail.com> wrote in message <12009090.1227870188030.JavaMail.jakarta@nitrogen.mathforum.org>...
> I am designing a device, which needs to communicate with MATLAB via serial port
>
> I need to have a CLI for taking values from the user which can be transmitted to the device. This has been done.
>
> I have now run into this problem...
> The device transmits a series of numbers which need to be plotted real time. Can this be done on MATLAB?
> The CLI also needs to be active and available at the same time.
>
> Could someone please point me in the right direction? Please feel free to offer any suggestions or ideas that you might have.
>
> I am new to MATLAB, but not to programming.

I'm not sure how you will get the data from the device to MATLAB, but one idea is to run a timer object that grabs data from the serial port object at the desired interval. It appears that the serial port object has a TimerFcn, so you can probably put the code to display the data in that callback.

As for plotting in real time, it sounds like you want a stripchart or the like. There is a nice package here: http://www.mathworks.com/matlabcentral/fileexchange/4722

Subject: Plotting a real time graph

From: arindam

Date: 28 Nov, 2008 14:43:17

Message: 5 of 12

Thank you Oliver and Ryan,

I now have an idea of how to proceed...
I will implement time interval based callbacks which will
  1) read the input buffer of the serial object
  2) callback the stripchart function
That should give me a real time plot with a rolling window
:)
If it works, I will share the results with you this Monday.
Thanks again

Subject: Plotting a real time graph

From: Elettro Saldatura

Date: 1 May, 2009 14:02:01

Message: 6 of 12

I am doing quite the same thing: trying to display data from an accelerometer with a serial interface.
The serial interface is quite simple
s=serial('comN');
fopen(s);

%then i send a command to the device
char;
fprintf( s,'adc%c',char);

%and then i get the values back
string=fgetl(s);

now the string has to be parsed (at least this is what i do: i convert char encoding to numbers), and you have your values.
For me it's a Vector3.

Now what i do to represent.
I do some math with the data (filtering and normalizing) and implement a simple delay: (non working code)

hline=plot
while(ishandle(hline))
    a=cputime;
    while(b<(a+0.01))
        b=cputime;
    end
    do whatever is necessary
end

in the plot you draw the first picture, then it is updated every time the "for" runs.
a+0.01 is the delay

Hope this helps.

Subject: Plotting a real time graph

From: Elettro Saldatura

Date: 1 May, 2009 14:14:02

Message: 7 of 12

"Elettro Saldatura" <erupter_remove_this_@tiscali.it> wrote in message
> in the plot you draw the first picture, then it is updated every time the "for" runs.
> a+0.01 is the delay

My error.
I meant the "while".
The program automatically quits when the graph window is closed.

Subject: Plotting a real time graph

From: Cristian Zivieri

Date: 10 Jul, 2009 12:54:01

Message: 8 of 12

arindam <goswamiarindam@gmail.com> wrote in message <23963555.1227883427711.JavaMail.jakarta@nitrogen.mathforum.org>...
> Thank you Oliver and Ryan,
>
> I now have an idea of how to proceed...
> I will implement time interval based callbacks which will
> 1) read the input buffer of the serial object
> 2) callback the stripchart function
> That should give me a real time plot with a rolling window
> :)
> If it works, I will share the results with you this Monday.
> Thanks again

Hello to everybody,

I've done the same thing as you: I receive data from a serial port (by USB converter) on my notebook and I plot them "real-time" with stripchart (it works good). My problem is that I receive data every 5 ms, so Matlab is not able to follow this tempistic and I observe a "real time" graphic with a big delay. In particular, each fscanf to read data needs about 20 ms.
The question is the follow: is there an instruction similar to fscanf but faster?

Thanks

Cristian Zivieri

Subject: Plotting a real time graph

From: Cristian Zivieri

Date: 10 Jul, 2009 12:54:01

Message: 9 of 12

arindam <goswamiarindam@gmail.com> wrote in message <23963555.1227883427711.JavaMail.jakarta@nitrogen.mathforum.org>...
> Thank you Oliver and Ryan,
>
> I now have an idea of how to proceed...
> I will implement time interval based callbacks which will
> 1) read the input buffer of the serial object
> 2) callback the stripchart function
> That should give me a real time plot with a rolling window
> :)
> If it works, I will share the results with you this Monday.
> Thanks again

Hello to everybody,

I've done the same thing as you: I receive data from a serial port (by USB converter) on my notebook and I plot them "real-time" with stripchart (it works good). My problem is that I receive data every 5 ms, so Matlab is not able to follow this tempistic and I observe a "real time" graphic with a big delay. In particular, each fscanf to read data needs about 20 ms.
The question is the follow: is there an instruction similar to fscanf but faster?

Thanks

Cristian Zivieri

Subject: Plotting a real time graph

From: Cristian Zivieri

Date: 10 Jul, 2009 12:54:02

Message: 10 of 12

arindam <goswamiarindam@gmail.com> wrote in message <23963555.1227883427711.JavaMail.jakarta@nitrogen.mathforum.org>...
> Thank you Oliver and Ryan,
>
> I now have an idea of how to proceed...
> I will implement time interval based callbacks which will
> 1) read the input buffer of the serial object
> 2) callback the stripchart function
> That should give me a real time plot with a rolling window
> :)
> If it works, I will share the results with you this Monday.
> Thanks again

Subject: Plotting a real time graph

From: sagarika g

Date: 15 Feb, 2010 15:20:05

Message: 11 of 12

im working in a similar way.im taking finger movement and catching those movements through accelerometer with a built in ADC in it and sending those sampled values into pc through RS232.how to record those sampled values while coming out of serial port and how to plot them. can u help me in the matlab code.......


"Elettro Saldatura" <erupter_remove_this_@tiscali.it> wrote in message <gtevcp$b0s$1@fred.mathworks.com>...
> I am doing quite the same thing: trying to display data from an accelerometer with a serial interface.
> The serial interface is quite simple
> s=serial('comN');
> fopen(s);
>
> %then i send a command to the device
> char;
> fprintf( s,'adc%c',char);
>
> %and then i get the values back
> string=fgetl(s);
>
> now the string has to be parsed (at least this is what i do: i convert char encoding to numbers), and you have your values.
> For me it's a Vector3.
>
> Now what i do to represent.
> I do some math with the data (filtering and normalizing) and implement a simple delay: (non working code)
>
> hline=plot
> while(ishandle(hline))
> a=cputime;
> while(b<(a+0.01))
> b=cputime;
> end
> do whatever is necessary
> end
>
> in the plot you draw the first picture, then it is updated every time the "for" runs.
> a+0.01 is the delay
>
> Hope this helps.

Subject: Plotting a real time graph

From: John

Date: 15 Feb, 2010 16:45:04

Message: 12 of 12

> im working in a similar way.im taking finger movement and catching those movements through accelerometer with a built in ADC in it and sending those sampled values into pc through RS232.how to record those sampled values while coming out of serial port and how to plot them. can u help me in the matlab code.......

I've done quite a bit of experimenting with a real-time GUI with an output graph and retrieving RS232 data to plot.

My solution was to use a number of timers set up in repeating, queued mode... these are the relevant ones:
1. Read serial data timer
Every X ms (I'm using 120ms), read all data in from the serial port and stuff it in a buffer. Then, parse the serial data buffer looking for data packets then place the data in the appropriate incoming data buffer - I have a number of separate data streams coming in that I graph separately, so each data stream has its own buffer.
3. Update graph timer
This checks if any of the graph data buffers was updated and, if so, it updates the graph.

There are a couple ways I found to keep things rolling smoothly:
1. Make sure the read serial timer doesn't take up all the processor: slow it down and increase the UART buffer if necessary to prevent overflows in between reads.
2. Don't try to update the graph too quickly; I have a drop box on my GUI to control the rate, so if it's having issues I slow it down.
3. Use the "drawnow" function to make sure the graph gets updated each time through. This will slow down the UART read timer if it takes too long.
4. Update the graph by setting the data line with the new data, not by calling "plot" each time; then, change the X-axis to the new max dimension. I.E.:
         set(plotHandle,'XData',Xindex,'YData',data.X);
         axis( handles.dat1Graph, [1 size(data.X,1) -100 100 ] );
5. The graph will update more quickly if you set the X-axis to be as long as you expect the data to be; then it won't have to rescale the axis each time through.

I hope that helps; I tried the stripchart but it didn't do what I wanted, as I wanted to be able to watch the incoming data build up, have smoothed lines, and be able to zoom in and out.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
realtime Oliver Woodford 28 Nov, 2008 08:25:08
plot Oliver Woodford 28 Nov, 2008 08:25:08
rssFeed for this Thread

Contact us at files@mathworks.com