append plot continuesly online
Show older comments
Hello I want plot data from text file, my file contains two columns 1.time and 2. Value,. Here my data continuesly append data to my text file with a time gap of 100 Milli seconds and I want to plot that append data to my plot continuesly with out closing plot figure
Answers (1)
Walter Roberson
on 16 Sep 2020
The plotting is not the problem. You can use
hold on
and plot() more data, or you can update the XData and YData properties of existing plotting objects (more efficient!), or you can use animatedline() and addpoints()
The real problem is in the detecting that additional data has finished arriving and reading that additional data.
If you read from a file that has no more data, then -1 (numeric!) is returned, and feof() is set (if it was not already set.)
In order to read more data from the same file, you either have to close and re-open the file, or else you have to fseek() to reposition in the file. Asking to fseek by 0 bytes forward relative to the current position is good enough to clear the end-of-file flag.
But if the other process is in the middle of writing data, then at the time you ask to fgetl() or fscanf() then it might not have a full line yet, so it might encounter end-of-file in the middle of what will later become a complete line once the writing is finished. There is more than one way to write data to a file. The most efficient way is write an entire block of data (typically either 4096 bytes or 8192 bytes) at a time as a single "atomic" I/O operation. The second most common is to write a line at a time as an "atomic" I/O operation; the third most common is to update after every character (inefficient!!)
If you are on MS Windows, you have the additional problem that you need to take extra steps to make it possible to read from a file that another process is writing. If you are on Linux or Mac, you do not need to take extra steps to make it possible to read from a file that another process is writing, but you do need to take extra steps to ensure that the file is in a consistent state before you use the new information.
These kind of consistency problems can often be made easier to deal with by not using a file for communications, and instead using TCP/IP for communications.
Categories
Find more on Just for fun in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!