Menu

Tabbed file -> array -> plot

Anonymous
2011-08-24
2012-09-07
  • Anonymous

    Anonymous - 2011-08-24

    Hi,

    I have a 3 column tabbed file (date, value, value) that I parse to an array
    which I use to plot a graph.

    There are no tabs at the end each lines.

    When I feed this array to SetDataValues() the result is no graph for the third
    (last) column. Only for the first 2(?) values of the third column.

    However when I add a tab to the end of each line (using sed), and I parse the
    file the array is exactly the same.

    But: I DO GET A GRAPH.

    I am confused how can phplot create different results with the same arrays?
    The file that are parsed are different (one has tabs eol and the other one
    doesn't) but the arrays are exactly the same.

    Arrays:

    http://www.willempasterkamp.nl/tab.php

    http://www.willempasterkamp.nl/tabeol.php

    Graphs:

    http://www.willempasterkamp.nl/tab.graph.php

    http://www.willempasterkamp.nl/tabeol.graph.php

    Cheers,

    Jan

     
  • lbayuk

    lbayuk - 2011-08-24

    Hi, could you post the code you use to parse the tabbed file and build your
    array? (Your links aren't helping because they are running as PHP scripts, I
    guess.) You can just paste it into this forum thread in a "code" block. I
    would like to take a look there first.

     
  • Anonymous

    Anonymous - 2011-08-25

    Hi Ibayuk,

    here it is.

    $graph = new PHPlot(1900,500);
    $examp = array();
    
    $file_handle = fopen("/tmp/test", "r");
    
    while (!feof($file_handle) ) {
        $line_of_text = fgets($file_handle);
        $parts = explode("\t", $line_of_text);
        array_push($examp, array("$parts[0]","$parts[1]","$parts[2]"));
    }
    fclose($file_handle);
    $graph->SetDataValues($examp);
    $graph->DrawGraph();
    
     
  • lbayuk

    lbayuk - 2011-08-25

    OK, the main problem is that fgets() returns a line from the file including
    the trailing newline. When you explode() that, the newline is still there, at
    the end of the last (3rd) field. That goes into your data array. PHPlot tests
    data values with is_numeric() to see if they are valid or should be skipped,
    and is_numeric("100\n") is false so that point gets ignored.

    Second problem is that testing with feof() at the top will result in one extra
    record being read at the end, containing no data. You probably should be
    testing the return from fgets() instead.

    You can fix the first problem by exploding rtrim($line_of_text) instead of
    $line_of_text.

    Or, you can use different functions to read the file. Here are some code
    snippets to read tab-separated values. But these read all fields on each line,
    which is different from yours which only reads the first 3. So they won't be
    usable to you if your file contains more columns that you want to ignore.

    This uses file() to read the whole file. If you have a huge file, it may

    use more memory than you want.

    $data = array();
    foreach (file($filename, FILE_IGNORE_NEW_LINES) as $line)
        $data[] = explode("\t", $line);
    

    Or, you can use fgetcsv() with delimiter "\t" to read the file. However this
    will strip out double quotes unless you also set the 'enclosure' parameter to
    something that never occurs in your file. For example:

    $fp = fopen($filename, "r");
    $data = array();
    while ($row = fgetcsv($fp, 0, "\t", "\001")) $data[] = $row;
    

    The 0 is for no line length limit, \t is the delimiter, and 'enclosure'
    argument is set to ASCII 1.

     
  • Anonymous

    Anonymous - 2011-08-26

    Wow thanks so much for your thorough explanation!

    It makes sense now. Thanks a lot.

     

Log in to post a comment.