Visualizing system from position data

In summary: the delimiter is carried over to the token
  • #1
MichalXC
41
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
  • #2
I also have Mathematica -- not sure how useful it might be in this case...
 
  • #3
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.
 
  • #4
I understand that conceptually, but I would be interested in looking at that parsing assignment -- my C skills are subpar. Thanks.
 
  • #5
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.
 
  • #6
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:
  • #7
Wow, that is amazing. I'll look through it (with a Mathematica 8 documentation tab open) and try to understand everything. Very much appreciated.
 
  • #8
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:

What is "Visualizing system from position data"?

"Visualizing system from position data" is a process where visual representations such as graphs or maps are created to display information about the movement and location of objects in a system. This can be used to analyze patterns, trends, and relationships within the data.

Why is it important to visualize system from position data?

Visualizing system from position data allows for a better understanding of complex data sets. It can help identify patterns or anomalies that may not be apparent when looking at raw data. It also allows for easier communication and presentation of the data to others.

What are some common methods for visualizing system from position data?

There are several methods for visualizing system from position data, including scatter plots, line graphs, heat maps, and geographic maps. These methods can be used to display different types of data and can be customized to fit the specific needs of the analysis.

What are some challenges when visualizing system from position data?

Some challenges when visualizing system from position data include dealing with large amounts of data, ensuring accuracy of the data, and choosing the most appropriate visualization method for the data. It is also important to consider the audience and their level of understanding when creating visualizations.

How can visualizing system from position data be used in scientific research?

Visualizing system from position data is commonly used in scientific research to analyze and interpret data from experiments or observations. It can help identify trends or correlations that may not be apparent through other analysis methods. It can also be used to communicate findings and results to other researchers and the general public.

Similar threads

  • Programming and Computer Science
Replies
11
Views
995
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
34
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
13
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
30
Views
4K
  • Astronomy and Astrophysics
Replies
1
Views
415
Back
Top