File Exchange

image thumbnail

Collect and plot data from an instrument in real time

version (2.33 KB) by Ankit Desai
Real time data collection & plotting using query based instrument interactions.


Updated 01 Sep 2016

View Version History

View License

This MATLAB(R) script generates a real time plot of voltage values collected from an Agilent(TM) 34401A Digital Multimeter over the serial port(RS232). The data is collected and plotted until the predefined stop time is reached. This example also demonstrates automating a query based interaction with an instrument while monitoring the results live.
This script can be modified to be used on any platform by changing the serialPort variable.

On Linux: serialPort = '/dev/ttyS0';
On MacOS: serialPort = '/dev/tty.KeySerial1';
On Windows: serialPort = 'COM1';

The script may also be updated to use any instrument/device to collect real time data. You may need to update the SCPI commands based on information in the instrument's programming manual.

To generate a report of this entire script, you may use the PUBLISH command at the MATLAB(R) command line as follows:


Cite As

Ankit Desai (2021). Collect and plot data from an instrument in real time (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (23)

yushi wang

Kenneth Stowe

Why does the time not start at 0?

zz d

Thanks a lot.If you can make it in a GUI,that will better.

merve kabak

sh H


All this zip file contains is a blank license.txt file.


Jibran Shahid

There is not code inside

Nguyen Thanh Duoc

Thanks a lot.

Shaymon Alvarez

Henri A

Can anyone kindly take a look at look my following errors. What should I do to get this to work please?

>> real_time_data_plot
Index exceeds matrix dimensions.

Error in real_time_data_plot (line 34)
serialObject = serial(serialPort);


Thanks a lot.
I test your code, and it's okay.
with condition i change:
voltage(count) = fscanf(serialObject,'%f',1); %#ok<SAGROW>

I don't now why? but result is good.

Thank a again.


Great code, nice and neat. Just wondering if you know whether you can run this concurrently with other processes, say, in a GUI? I want to have this logging every few seconds and in between, pass control back to allow other processes to run.
Great work

An Nguyen


Hello Ankit,

Thank you very much for sharing this with us. I'm trying to read in sonar data via UART continuously over the serial port. I am having speed issues, however, after a certain amount of time.

Would you happen to know if this is a buffer issue? There doesn't seem to be any active cleaning as count increases. I tried a quick fix to reset count back to 1 after 200 iterations which does update correctly but the speed issue still persists.

Any insight would be greatly appreciated!


and..sir....i dunno to decide...if i should show the three heat variations in a single graph or to create three separate graphs to show induvidual variation...please help me to finish the code sir...

Ying Zhou

Ying Zhou

Thanks for ur work. It's very useful. I have a pressure sensor to measure air pressure. I use Arduino uno to do ADC and connect to PC. Send data trough serial port to PC. Just made a little change of ur code I got real-time plot of my sensor data successfully.

Ying Zhou

Ankit Desai

You are getting that error since the fscanf on the serial object did not return any data. You might want to put in a check to make sure you are getting data in each iteration. Hope this helps.

Christian Franco

Hallo and thank you for your work. When i run the file it prompts an error that said

??? In an assignment A(I) = B, the number of elements in B and
I must be the same.

Error in ==> real_time_data_plot at 81
voltage(count) = fscanf(serialObject,'%f'); %#ok<SAGROW>

thank you

Diego Barragán

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!