Visualizing system from position data

Click For Summary

Discussion Overview

The discussion revolves around the challenge of visualizing a system of 2-D disks using position data extracted from text documents. Participants explore programming approaches, particularly in C and Mathematica, to parse the data and generate visualizations at specific time instances.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant describes the structure of the data in the text documents and expresses the need to extract position and diameter information for visualization.
  • Another participant suggests using a C program to read and parse the text file, mentioning methods like string tokenization and character-by-character parsing.
  • A later reply offers a Mathematica code snippet that demonstrates how to read and extract data from a text file, emphasizing the importance of understanding each function used.
  • Some participants express uncertainty about string tokenization and how to handle strings in C, seeking clarification on these concepts.
  • Examples of using the strtok() function in C are provided to illustrate how to split strings based on delimiters.
  • Further code examples are shared to demonstrate manual parsing of characters from a file, highlighting the need for additional logic to manage data extraction effectively.

Areas of Agreement / Disagreement

Participants generally agree on the need to parse the data for visualization, but there are multiple approaches discussed without a consensus on the best method. Uncertainty remains regarding the implementation details and the effectiveness of different programming techniques.

Contextual Notes

Participants mention limitations in their programming skills and express the need for more examples and explanations to fully understand the parsing and visualization process.

MichalXC
Messages
39
Reaction score
0
Hi,

I have a bunch of text documents, each of which contains (among other information) the x- and y-positions of 1,000 different 2-D disks at some given time. I want to write a code in C which takes each text document and outputs a visualization of the system at that instant in time.

The data in the text documents is arranged like this:

[time] [particle diameter] [x-position] [y-position] [x-velocity] [y-velocity] [etc...]

If I could only extract and visualize the position and diameter information, I would have a "snapshot" of the system at a given instant. (I will eventually string together the snapshots to see the motion of the system.)

What would be the best way of going about this from a programming perspective? I've written .ppm files using C code before to visualize the Mandelbrot set... Would that be good? How can I extract and draw the relevant information (position and diameter)? Sample code is appreciated.

Thanks.
 
Technology news on Phys.org
I also have Mathematica -- not sure how useful it might be in this case...
 
You can write a C program to open the text file and parse each line, extracting the values of each parameter. It's not overly complex, depending how much C you know.

There are different approaches, but you could use the string tokenizer to recognise the space delimiter between each parameter on a line, or you could parse the line one character a time until you find the delimiter, in each case you will be left with a string that represents one parameter, which you can save as a string, or convert to a number, or whatever is appropriate.

It's good practise terminating your own strings, checking validity, converting between types, allocating memory correctly and whatnot. :)

Once you have the data extracted, you're free to use it how you wish, generate your own visualizations, etc.

I have a string processing assignment somewhere, I'll see if I can dig it out and show you some code but not today I'm out of time.
 
I understand that conceptually, but I would be interested in looking at that parsing assignment -- my C skills are subpar. Thanks.
 
The "space delimiter" method sounds nice. I have no idea what a string tokenizer is, and I'm not sure how to save something as a string, or what that means.
 
Perhaps you can adapt this.

Given the file fqq.txt in my Mathematica folder containing

1 2 3 4 5 6 7 8 9
2 1 4 3 5 7 6 8 4
3 3 4 1 5 6 7 8 2

This should read and extract your desired data.

In[1]:= s=OpenRead["fqq.txt"];
data=Reap[
While[(w=Read[s,Record])=!=EndOfFile,
Sow[ToExpression[StringJoin["{", StringReplace[w, " " -> ","], "}"]]];
]
][[2,1]];
Close;
Print[data];
diaxy=Map[Take[#,{2,4}]&,data];
Print[diaxy];

From In[1]:= {{1,2,3,4,5,6,7,8,9},{2,1,4,3,5,7,6,8,4},{3,3,4,1,5,6,7,8,2}}

From In[1]:= {{2,3,4},{1,4,3},{3,4,1}}

Go through that a character at a time using the help system to understand each function until you know how that is working.
 
Last edited:
Wow, that is amazing. I'll look through it (with a Mathematica 8 documentation tab open) and try to understand everything. Very much appreciated.
 
MichalXC said:
The "space delimiter" method sounds nice. I have no idea what a string tokenizer is, and I'm not sure how to save something as a string, or what that means.

OK for starters, a string is a sequence of characters. As opposed to a number which is treated differently. If you do this in C, you need a way to take those characters from your input file and at least in some cases, convert them to a number.

Now for string tokenizing. strtok() is a function that takes as input a string and a delimiter(s) and essentially splits the big string into small strings. It's not super-intuitive to use, so here's an example:

Code:
/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}

[URL]http://www.cplusplus.com/reference/clibrary/cstring/strtok/
[/URL]

As you can see, str is your starting string, pch is a pointer to char which will contain a reference to the current small string. The first call to strtok() splits str (it actually changes str and replaces the delimiter with '\0' (NULL char). Successive calls to strtok() (in a loop) do not supply the starting string str, and instead use NULL, and pch will be assigned the reference to the next sub-string until it hits the end.

It's worth reading the man page, I fear my explanation is flawed. Here's an excerpt:

The strtok() function parses a string into a sequence of tokens. On
the first call to strtok() the string to be parsed should be specified
in str. In each subsequent call that should parse the same string, str
should be NULL.

The delim argument specifies a set of characters that delimit the
tokens in the parsed string. The caller may specify different strings
in delim in successive calls that parse the same string.

Each call to strtok() returns a pointer to a null-terminated string
containing the next token. This string does not include the delimiting
character. If no more tokens are found, strtok() returns NULL.

Another option is to look at each character individually and parse the line manually. Here's some code:

Code:
FILE *input_file; // a pointer to your input file

int input_char; // the current char (fgetc returns an int, not a char, but this is not really important)
char buffer[20]; // a place to store the substring
int index = 0; // current place in the buffer
char delimiter = ' '; // space character

input_file = fopen("myfile.txt", "r"); // open the file for reading
if(input_file == NULL)
    exit(1); // file open failed.

while((input_char = fgetc(input_file)) != EOF) // while we are not at EndOfFile
{
    if(input_char != delimiter) // if the char is not the delimiter
    {
        buffer[index] = input_char; // store it in the buffer
        index++; // increment the index
    }
    else
    {
        buffer[index] = '\0'; // manually terminate the substring!

        // let's assume you want the first substring time stored as an integer

        int time = atoi(buffer); // atoi() converts a string to an int.
    }
}

fclose(input_file); // close the file.

This is an incomplete piece of code, you need more logic to know where you're up to in the string, to treat each piece of data appropriately, clear the buffer, etc. but I hope it illustrates the point.
 
Last edited by a moderator:

Similar threads

Replies
4
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
4K
Replies
13
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K