Count a specific value in a given data in csv file

I'm struggling with the CSV file saved in the SDcard on how I count a specific number and assign them to a variable. I'm logging data coming from sensors and I want to display it in a graph sooner. Each line consist of 10 values wherein out of those values the 8th values have a data of (43,44 and 45) and I want to count each given data. Please see the below data.

1,21,300,1700,2,10,55,45,11,11
1,21,300,1800,2,10,56,44,12,19
1,21,300,1700,2,10,56,43,11,28
1,21,300,1800,2,11,05,45,10,11
1,21,300,1700,2,10,18,43,11,12
1,21,300,1800,2,12,25,44,11,11
1,21,300,1800,2,12,25,44,11,11

Output:

43 = 2
44 = 3
45 = 2

Could someone be so kind to shed me some light on this?

Put a line into an array. Iterate through the array with a for loop. Compare each element with 43. If the element is 43, increment a variable, say count43. Do the same with 44 and 45 with variables like count44 and count45.

Here is code to illustrate what I said. I don't have an SD card handy so I put the data into a 2D array. I pull one line out of the array into a temporary array and iterate through the temp array checking each element against 43,44 and 45 incrementing the corresponding counter if the element is equal to one of the numbers.


int data[][10] =
{
   {1, 21, 300, 1700, 2, 10, 55, 45, 11, 11},
   {1, 21, 300, 1800, 2, 10, 56, 44, 12, 19},
   {1, 21, 300, 1700, 2, 10, 56, 43, 11, 28},
   {1, 21, 300, 1800, 2, 11, 05, 45, 10, 11},
   {1, 21, 300, 1700, 2, 10, 18, 43, 11, 12},
   {1, 21, 300, 1800, 2, 12, 25, 44, 11, 11},
   {1, 21, 300, 1800, 2, 12, 25, 44, 11, 11}
};

int count43 = 0;
int count44 = 0;
int count45 = 0;

void setup()
{
   Serial.begin(115200);
   int dataSize = sizeof(data[0]);  // get the size in bytes of the data array
   //Serial.println(dataSize);

   // get one line of the data 
   for (int n = 0; n < 7; n++)
   {
      int temp[dataSize];    // a temp array to work on
      memcpy(temp, data[n], dataSize); // copy the line to a temporary array

      // iterate through the line an element at a time   
      for (int m = 0; m < 10; m++) 
      {
         if (temp[m] == 43) // compare with 43
         {
            count43++;  // if 43 increment counter
         }
         if (temp[m] == 44) // compare with 44
         {
            count44++;  // if 44 increment counter
         }
         if (temp[m] == 45) // compare with 45
         {
            count45++;  // if 45 increment counter
         }
      }
   }
   Serial.print("43 count = ");
   Serial.println(count43);
   Serial.print("44 count = ");
   Serial.println(count44);
   Serial.print("45 count = ");
   Serial.println(count45);
}

void loop()
{
}

Is this what you want or are you only interested in the element 7 (eighth value) column?

i will be counting also on how many (43,44,45) in 4th element also. like this.

1700: 43=2, 44=0, 45=1
1800: 43=0; 44=3, 45=1

Thanks a lot.

The code that I posted will count the target numbers in the line (all columns).

You could use the Stream method find().
Something like this should work (not tested)

int counter = 0;
while(file.find(target)) {
   counter++;  
}

Tested, it works.

You can write a function with target string as input parameter and counter value as result.

int getOccurrence(const char* _target){
  File file = LittleFS.open("/test.txt", "r");
  int counter = 0;
  while(file.find(_target)) {
    counter++;
  }
  return counter;
}

This topic was automatically closed after 120 days. New replies are no longer allowed.