plotting data

edited March 29 in Android Mode

Hello, I am a new one in programming and i would like your help.I want a code in processing i3 that could read a txt or csv file of data and make a real graph.Especcially to read ecg signals and make a cardiograph.Thanks a lot..

«134

Answers

  • edited November 2017

    Please check prev posts: https://forum.processing.org/two/search?Search=ECG

    Also provide the code you have so far. If you are looking for somebody to do the work for you, there is a section called Events & Opportunities.

    ****EDIT: Referring to jeremydouglass'es comment, here is one post for example: https://forum.processing.org/two/discussion/136/grafica-library#latest

    Kf

  • Also try scrolling through the last 2-3 search pages of a forum search for the grafica library:

    https://forum.processing.org/two/search?Search=grafica

  • i have learned hot to load and store a tsv or a csv file but i don't know how to plot in real time axis..What do you mean about the section '' Events & Opportunities''? @kfrajer

  • please show me a way or tell me a book that i can read to learn about plotting data..

  • It is not possible to know what level of Proceessing you have based on your post. If you provide some code of what you can do, then one could take it to the next step. If you are new in Processing, you can start by looking at the reference in the Processing's website. Check the following keywords: draw() and setup(). Then you should consider checking the example section in the same site, or the tutorials.

    If you want to something in the forum, it is better to do the search from google as the search feature in this forum is not robust.

    Kf

  • Re:

    i don't know how to plot in real time axis

    ...I don't think you actually looked at the posts I shared.

    https://jagracar.com/sketches/multiplePlots.php

    Move your mouse around and watch the "Mouse Position" sketch plot in real time.

  • edited November 2017

    Ecg[] ecg; // A Table object Table table;

    void setup() { size(640, 360); loadData(); } void draw() { background(255); } void loadData() { // Load CSV file into a Table object table = loadTable("data.csv", "header"); ecg =new Ecg[table.getRowCount()]; //plot(ecg); //plot(ecg(20001:30000)); int rowCount = 0; for (TableRow row : table.rows()) { // You can access the fields via their column name (or index) float x = row.getFloat("x"); float y = row.getFloat("y"); ecg[rowCount] = new Ecg(x, y); rowCount++; }

  • Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    Related to your data, do you want to plot all the data at once? Or in sections?

    I suggest you install the Graphica library (via Library Manager right inside the Processing IDE) and explore the provided examples. If you find an example that you think it does a good job, or an example that fits your vision, share it here and you will get more feedback. There is not enough information to provide you with any useful feedback because in your case, the "meat" is in the details.

    Kf

  • I want to plot all my data at once, to load a csv or tsv file with thousands of rows in toy axes...I have installed the grafica library and i tried the examples from the site and nothing..I have no result..

  • edited January 16

    The code bellow is about to load a csv file and make a real ECG graph. I can not find what i a am doing wrong.Please help me..

    import grafica.*;
    import java.util.*;
    import java.text.*;
    
    //SimpleDateFormat Elapsedtime = new SimpleDateFormat ("mm-ss-mmm");
    float Elapsedtime[]=new float Elapsedtime[3];
    float ECG [] = new float ECG[3];
    
    GPlot plot;
    
    void setup() {
      size(800, 410);
    
     Table table = loadTable("samples.csv", "header");
     table.setColumnType("Elapsedtime", Table.FLOAT);
      table.setColumnType("ECG", Table.FLOAT);
    
    
      GPointsArray points = new GPointsArray();
      for (int row = 0; row < table.getRowCount(); row++) {
       float Elapsedtime = table.getFloat(row, "Elapsedtime");
        float ECG = table.getFloat(row, "ECG");
       points.add(Elapsedtime,ECG);
    
         }
    // Create the plot
      plot = new GPlot(this);
      plot.setDim(700, 300);
       plot.getXAxis().setNTicks(0);
      plot.setPoints(points);
    
    }
    
    void draw() {
      background(255);
    
      // Draw the plot  
      plot.beginDraw();
      plot.drawBox();
      plot.drawXAxis();
      plot.drawYAxis();
      plot.drawTitle();
      plot.endDraw();
    }  
    
    } 
    
  • can you post the first 10 lines of your csv so we can run your code with it....?

    Thanks

  • Also the form of my csv file samples.csv is: 'Elapsedtime','ECG' hh:mm:ss.mmm','mV' 0.000,0.185 0.001,0.185 0.003,0.169

  • 10 lines please formatted as code - thanks!

  • edited January 16
    'Elapsed time','ECG'
    'hh:mm:ss.mmm','mV'
    '0:00.000',0.185
    '0:00.001',0.185
    '0:00.003',0.169
    '0:00.004',0.185
    '0:00.006',0.185
    '0:00.007',0.155
    '0:00.008',0.185
    '0:00.010',0.215
    
  • Did you look at the examples for grafica?

    Do you plot the points in draw()?

  • Of course i looked them.Also my code is based on Oktoberfest example.

  • MAYbe you should delete line 2 from the csv

    Ain‘t numbers

  • edited January 16

    Βut i want the measured units in my axis. I delete the second line from the csv but still the same.. I can not capture the points in the graph.

  • In your code: Table table = loadTable("samples.csv", "header");

    From the reference: https://processing.org/reference/loadTable_.html

    If the file contains a header row, include "header" in the options parameter. If the file does not have a header row, then simply omit the "header" option.

    It is implied that when a file has a header, the header is only one line and it is the first line in the input file. However, your CSV file has two header lines. You have two options:

    1. Remove the header from the file manually using your favorite text editor, as Chrisir suggested.

    2. Read the table without the header option. Then process the first two lines as string parameters. Then process the rest of the file as numeric lines (line 20, your row counter starts at 2, not 0 then). If you proceed with the second option, then I don't think you can set each column to a float type (Line 21+22 in your last post). Instead, assign them as Strings and then, when reading the numeric values as strings, parse them to the proper numeric type.

    Kf

  • @kfrajer what do you mean when you say , reading the numeric values as strings, parse them to the proper numeric type??

  • Your lines 21 and 22

  • edited January 16
    import grafica.*;
    import java.util.*;
    import java.text.*;
    
    //SimpleDateFormat Elapsedtime = new SimpleDateFormat ("mm-ss-mmm");
    String Elapsedtime[]=new String Elapsedtime[8];
    String ECG [] = new String ECG[8];
    
    GPlot plot;
    
    void setup() {
      size(800, 410);
    
     Table table = loadTable("samples.csv");
     table.setColumnType("Elapsedtime", Table.STRING);
      table.setColumnType("ECG", Table.STRING);
    
    
       GPointsArray points = new GPointsArray();
      for (int row = 1; row < table.getRowCount(); row++) {
      String Elapsedtime = table.getString(row, "Elapsedtime");
      String ECG = table.getString(row, "ECG");
    
       points.add(Elapsedtime,ECG);
    
     }
    // Create the plot
      plot = new GPlot(this);
      plot.setDim(700, 300);
      plot.getXAxis().setNTicks(0);
      plot.setPoints(points);
    
    }
    
    void draw() {
      background(255);
    
      // Draw the plot  
      plot.beginDraw();
      plot.drawBox();
      plot.drawXAxis();
      plot.drawYAxis();
      plot.drawTitle();
      plot.drawPoints();
      plot.endDraw();
    } 
    
  • Does it work? Solved?

    You can use ctrl-t in processing to get automatically better indents

  • edited January 16

    @Chrisir No, it does not work..I cant find the mistake.. The result after i run the code is below:

  • There are several points where your error can be.

    Try to narrow it down

    Line 24 would be my main suspect

    Try ....add(row, 4.0); instead

    What’s the result when you run it?

  • this is the result:

    Χωρίς τίτλο

  • edited January 16

    It worked!!! Okay, we picked the right line!!!

    Now go on and try

    ....add(row, ECG);

    I guess it won’t work since ECG is a String (not a float)

    Next try:

    ...add(row, float(ECG));

    Then you need to replace row with your time stamp but this probably needs to be converted to float somehow.

    and just float(...) won’t do this. You need to convert the time stamp to millis or 1/10 second and then to float.

  • The problem is that i cant show the x coordinate which is the Elapsedtime.. I dont know in which form should i initialize it and how will it appears in x axis. I remind you my data:

    Elapsedtime,ECG
    '0:00.000',0.185
    '0:00.001',0.185
    '0:00.003',0.169
    '0:00.004',0.185
    '0:00.006',0.185
    '0:00.007',0.155
    '0:00.008',0.185
    '0:00.010',0.215
    

    Is there a function that counts time in hours,minutes,seconds and milliseconds??Do i have to initialize the Elapsedtime somehow as time that the programm recognise time and starts to count as long as i get ECG signals?

  • Did you test the steps I outlined? Does ECG work now?

  • edited January 16

    You can use

    String[] timeElements=split(Elapsedtime,‘:‘);

    then work on with timeElements[1]

    float temp1=float(timeElements[1] );
    
    float temp2=map(temp1, 0, 1, 0, 500);
    

    try temp2 instead of row in add():

    ...add(temp2, .....

  • edited January 16

    Look up split and map in the reference

  • Solved..?

  • So this is my finally code..After running that it shows me en error : map(NaN, 0, 1, 0, 500) called, which returns NaN (not a number) As you will see below i tried everything that you have said to me..Also i tried instead of spit the function spitTokens() or trim() and it is the same result.

    import grafica.*;
    import java.util.*;
    import java.text.*;
    
    
    GPlot plot;
    
    void setup() {
      size(800, 410);
    
      Table table = loadTable("samples5.csv","header");
      table.setColumnType("Elapsedtime", Table.STRING);
      table.setColumnType("ECG", Table.FLOAT);
    
    
      GPointsArray points = new GPointsArray();
    
    
     for (int row = 1; row < table.getRowCount(); row++) {
        String Elapsedtime = table.getString (row, "Elapsedtime");
        String[] timeElements=split(Elapsedtime,':');
        float temp1=float(Elapsedtime);
        float temp2=map(temp1, 0, 1, 0, 500);
        float ECG = table.getFloat(row, "ECG");
    
        points.add(temp2,ECG);
    
      }
      // Create the plot
      plot = new GPlot(this);
      plot.setDim(700, 300);
      plot.getXAxis().setNTicks(0);
      plot.setPoints(points);
      plot.setPointColor(color(100, 100, 255, 50));
    }
    
    void draw() {
      background(255);
    
      // Draw the plot  
      plot.beginDraw();
      plot.drawBox();
      plot.drawXAxis();
      plot.drawYAxis();
      plot.drawTopAxis();
      plot.drawRightAxis();
      plot.drawTitle();
      plot.drawPoints();
      plot.drawGridLines(GPlot.VERTICAL);
      plot.drawFilledContours(GPlot.HORIZONTAL, 0);
      plot.endDraw();
    

    }

  • This picture below is my graph after i run the code. And as you can see it can not recognise the x axis, the Elapsedtime.

    3

  • edited January 16

    Hello,

    sorry, that this is so annoying.

    You did a very good job!

    • that was wrong: you had: float temp1=float(Elapsedtime);

    • I had float temp1=float(timeElements[1] );

    but I forgot to write Elapsedtime = Elapsedtime.replace( "'", ""); before that to remove the little ' signs in the first column (all of them):

    Elapsedtime,ECG
    '0:00.000',0.185
    '0:00.001',0.185
    '0:00.003',0.169
    '0:00.004',0.185
    '0:00.006',0.185
    '0:00.007',0.155
    '0:00.008',0.185
    '0:00.010',0.215
    

    so I overlooked the '

    Sorry about that.

    entire sketch works now:

    import grafica.*;
    import java.util.*;
    import java.text.*;
    
    
    GPlot plot;
    
    void setup() {
      size(800, 410);
    
      Table table = loadTable("samples5.csv", "header");
      table.setColumnType("Elapsedtime", Table.STRING);
      table.setColumnType("ECG", Table.FLOAT);
    
    
      GPointsArray points = new GPointsArray();
    
    
      for (int row = 1; row < table.getRowCount(); row++) {
        String Elapsedtime = table.getString (row, "Elapsedtime");
    
        // remove ' from first column 
        Elapsedtime = Elapsedtime.replace( "'", ""); 
    
        // split at : 
        String[] timeElements=split(Elapsedtime, ':');
    
        // use 2nd entry of new array
        float temp1=float(timeElements[1]);
    
        // map to a better range
        float temp2=map(temp1, 0, 1, 0, 500);
        float ECG = table.getFloat(row, "ECG");
    
        points.add(temp2, ECG);
      }
      // Create the plot
      plot = new GPlot(this);
      plot.setDim(700, 300);
      plot.getXAxis().setNTicks(0);
      plot.setPoints(points);
      plot.setPointColor(color(100, 100, 255, 50));
    }
    
    void draw() {
      background(255);
    
      // Draw the plot  
      plot.beginDraw();
      plot.drawBox();
      plot.drawXAxis();
      plot.drawYAxis();
      plot.drawTopAxis();
      plot.drawRightAxis();
      plot.drawTitle();
      plot.drawPoints();
      plot.drawGridLines(GPlot.VERTICAL);
      plot.drawFilledContours(GPlot.HORIZONTAL, 0);
      plot.endDraw();
    }
    
  • This picture below is my graph after i run the code. And as you can see it can not recognise the x axis, the Elapsedtime.

    I hope it's better now.

  • No still the same problem...Nothing different.. X_X :( :(

  • ??

    My code runs smoothly with the data (10 lines) you provided

    Did you try this?

    Does your data change after the first lines?

  • edited January 17

    No,i did not change my data..They are the same as the first 10 lines. It can not shows numarated the x axis..This is my result:

    9

  • Also i tried a simpler example and as you see the problem is the same, no numbers in x axis... The simpler example is here:

    https://forum.processing.org/two/discussion/26003/how-can-i-numerate-x-axis-in-a-graph#latest

  • Ah, I am happy to see the actual graph is there!!!

    What’s missing now is only the numbers on x-axis....?

  • Everything is ok except the x-axis, that i can not understand where or what correspond the numbers 1,2,3,4,5 in the x axis..What conversion happend from this time form '0:00.000','0:00.001','0:00.003','0:00.004','0:00.006',.. to 1,2,3,4... numbers that they are shown to the x-axis???

    3

  • edited January 27

    Line 32? Above the line with map()

    Look at map() in the reference

  • Hello,

    i fixed this programm and it works perfect in java mode.Thank you very much @Chrisir...I appreciate very much your help.. Now i want to turn this code to android mode and make it an app but it can not recognise the grafica library..As i read here in the forum, grafica library is not compatible with android mode..Is there a way to make it compatible or must i change the library?

  • I don’t know

    Write the author of the library

    Check its website

  • I wrote to him but i have no answer..I dont know from where and how will i be helped.

  • Check if there are other libraries for mathematics/ plotting

    Or you have to google

    Or write your own class

  • if i find and change the library, must i change the code?? How will i know if the new library is compatible with android mode?

Sign In or Register to comment.