Message Boards Message Boards

0
|
5060 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:
GROUPS:

Displaying realtime data

Posted 11 years ago
Hello,

I would like to ask you a question regarding Mathematica's abilities to plot realtime data. I would like to periodically write some data say in a file in CSV format and I would like to display the data in this file as live-graphs in Mathematica. The refresh interval would be, say 2 seconds. So the question is, whether it is possible to display 2D/3D graphs/plots in mathematica with an auto-update, say every 2 seconds. The data would be located in an external file.

Thank you very much.

Yours sincerely,
Tomas Selnekovic
POSTED BY: Tomas Selnekovic
2 Replies
Posted 11 years ago
This is my try:
 createFileHandle[filename_] := {filename, 0};
 readNewLines[{filename_, pos_}] :=
   Module[{f, str},
     f = OpenRead[filename];
     SetStreamPosition[f, pos];
     str = StringCases[StringJoin@ReadList[f, Character], Longest[StartOfString ~~ ___ ~~ ("\n" | "\r")]];
     Close[f];
     If[str == {},
       {{filename, pos}, ""},
      {{filename, pos + StringLength@First@str}, First@str}]];
It reads complete lines from a file, continuing from position of latest seen beginning of line on every try. The position logic probably breaks if your input has multi-byte characters (that is, it's anything beyong plain ASCII or eight-bit encoded input). If line delimiter used in your system is "\r\n" (CR LF - used by Windows), you probably want to use that instead of ("\n" | "\r") in StringCases to further avoid getting empty records.

You can view dynamic plot if you add new lines of CSV data to the test file test.txt:
DynamicModule[{f = createFileHandle["test.txt"], data = {}, newstrings},
  Dynamic[
    {f, newstrings} = readNewLines[f];
    data = data~Join~ImportString[newstrings, "CSV"];
    ListLinePlot[data],
    UpdateInterval -> 1]]
POSTED BY: Jari Kirma
Jari, thank You for the solution. I adapted it a little and it works.
POSTED BY: Tomas Selnekovic
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract