File Exchange

image thumbnail

Collect and plot data from a TCPIP server in real time

version (2.22 KB) by Ankit Desai
Obtain real time data from a TCPIP object using Instrument Control Toolbox and visualize it live.


Updated 01 Sep 2016

View License

This example demonstrates how to automatically read a set number of data bytes as and when they are available. This MATLAB® script also generates a real time plot of streaming data collected from the TCPIP server.
The script may be updated to use any instrument/device/TCPIP server to collect real time data. You may need to update the IP address and port.

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


Comments and Ratings (26)

I have downloaded file and changed the IP address and port number but it is not showing any thing , please suggest why it is not showing any thing.

kin wen

Hello ANkit, i got this error once i run the script
Error using tcpip
License checkout failed.
License Manager Error -4
Maximum number of users for Instr_Control_Toolbox reached.
Try again later.
To see a list of current users use the lmstat utility or contact your License Administrator.

Troubleshoot this issue by visiting:

Diagnostic Information:
Feature: Instr_Control_Toolbox
License path: C:\Users\Kinan\AppData\Roaming\MathWorks\MATLAB\R2015b_licenses;C:\Program Files\MATLAB\R2015b\licenses\license.dat;C:\Program
Licensing error: -4,132.

Error in real_time_data_stream_plotting (line 27)
interfaceObject = tcpip('localhost',19);
** Please can you help to get the answer**


Hello Ankit,
I tested your code on a Rigol oscilloscope to receive streaming on matlab, but it does not work?!
I do not know where the problem,
At first it gave me an error at line 66 in function "fopen" and there I change the entry "localhost" with the IP address of the camera with a port "5555" but it gave neither error nor result!
what is the solution in your opinion?

zip file is empty
license.txt 0 bytes

meng meng

Thinks for your work,I'm looking for this kind of example...

Laura Lippi

Hello Ankit,
I have a problem to plot data. When I try to plot data, I use this part of code:


but it doesn't plot my data ax, but it refreshes the data' values only on the y-axis of the plot.
Thank you very much for your help!

John Booker


Chau Nguyen


che adil

hello ankit,
I tested your code on an ip camera to receive streaming on matlab, but it does not work?!
I do not know where the problem,
at first it gave me an error at line 66 in function "fopen" and there I change the entry "localhost" with the ip address of the camera with a port "8001" but it gave neither error nor result!
knowing that the camera is normally secured by password, what is the solution in your opinion?

Luis Celi

It doesn’t plot anything! I think that doesn't work because it isn’t a numeric data... I proof that making a ping to another computer and doesn’t plot anything How I can try? Thanks...

I want to read live video stream from my media server over LAN in there any way to do this? Actual setup is like this: There is survelliance equipment.Using TV tuner card and media encoder,we are streaming it over LAN to other users. Users can enter url(IP address of media server and port number) in their media player and can view the live feed. We want to read this feed in MATLAB so that we can carry out image processing on this stream.

hi can I use this code to read live video stream from Media Server?

hi can I use this code to read live video stream from Media Server?

Ankit Desai

Hi Abhishek,

You might have to replace the "~" with a variable name. The "~" is used to denote a variable that is never used in the code.

The callback function always takes in parent object and event as the first two arguments. However, since I am not using the event variable in the callback function, I replaced it with "~". Once you put a variable name in it's place, it should work fine.

Hope this helps,


Hi Ankit,
I am running your code in the version number R2008a.
I got the following error
Error: File: real_time_data_stream_plotting.m Line: 70 Column: 43
Unexpected MATLAB operator.

Please clarify...


Hello Ankit,
Can I use this program in Matlab version 2007b?


Hi Ankit,

Thanks for your useful script. I got it work; however I still do not know how to get the data which are being read by the TCPIP server.
In other words, using your file, I can stream and plot all those data, but I need to demonstrate more results, so I require the data at each instant. I am wondering how I can access those data?


Ankit Desai

Ghani, the error might be due to corrupted path. Try to reset MATLAB path and see if that works.

It might also give you this error if you have created your own plot function which takes different number of inputs. Hope this helps.

Ghani Amna

Hi... I also ran this code,but it's showing error.

??? Error using ==> plot
Too many input arguments.

Error in ==> real_time_data_stream_plotting at 51
plotHandle = plot(axesHandle,0,'-y','LineWidth',1);
Any Help...?

John Luthar

That worked great!

I ran into another problem I can't figure out.
The tcpip object has a communication state that shows closed/open.

I'm currently using Matlab as a client connected to a server. If the server goes down, the communication state (connection.status) doesn't change from open -> closed.

Once it connects, the status doesn't seem to change again. How would I check if the connection ever went down?

Ankit Desai

You might also have to update the TCPIP object's InputBufferSize and OutputBufferSize properties along with bytesToRead variable in the program to the desired number. Hope this helps.

John Luthar

Is it possible to read more than 500 bytes on the call-back?
I can't seem to get that working.


Updated license

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

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor