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 10

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 10

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 10

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 10

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 10

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 10

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 10

"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 10

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 10

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 10

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

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
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com