how do I create a datafile with numbers separated by TAB's

Hi,
I would like to read out 6 sensors and get a datafile. This file I would like to use in excel for analysis. But in the code I use numbers are splitted by a “,” in a string and when copying this to excel it do not become nice columns with sensordata… I have now done it in a bold way by separating each value by a println, but I think it can be more nicely done. Who helps!
(I am not experienced at all, stil learning)

void loop()
{
// make a string for assembling the data to log:
String dataString = “”;

// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 6; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 5) {
dataString += “,”;
}
}

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open(“datalog.txt”, FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn’t open, pop up an error:
else {
Serial.println(“error opening datalog.txt”);
}
}

But in the code I use numbers are splitted by a "," in a string and when copying this to excel it do not become nice columns with sensordata.

Excel can be told to use different separators when reading text files. The comma is quite common - the csv extension stands for comma-separated values - and excel can read csv files.

The character '\t' will put a tab in the file wherever it is used.

But in the code I use numbers are splitted by a "," in a string

You put the ASCII code for a tab in between the numbers instead of putting the ASCII code for a comma between the numbers.

Don

On the assumption that the datafile println works like the strin, then just use "\t" instead of "," and it will be a tab character.

BTW, with Excel you can adjust if ",", tab (or anything else) is the field seperator, so "," should be fine anyhow.

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 6; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor) + "\t";
  }

Not necessary to add the TAB conditionally => it would just give an empty colum in Excel.

Furthermore you could put the writing to file in a separate function: e.g. void LogData(String s) so it can be reused
e.g. from setup() where you could send a headerline to the log file so it is clear what the columns mean

Great. Thanks, "/t " did the trick.