Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Discussion Writing position data to a csv file

Discussion in 'Scripting' started by VCUMCL, Jul 10, 2023.

  1. VCUMCL

    VCUMCL

    Joined:
    Jun 5, 2017
    Posts:
    24
    I am creating a VR project where the position data of the headset and the virtual representation of a haptic stylus will need to be recorded to a csv file. I have a script set up that fetches this data and should write it to the file:

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections.Generic;
    4. using System.IO;
    5.  
    6. public class WritePositionData : MonoBehaviour
    7. {
    8.     private string positionDataFilePath;
    9.     public GameObject Head;
    10.     public GameObject Stylus;
    11.     private List<string> positionData;
    12.  
    13.     void Start()
    14.     {
    15.         positionData = new List<string>();
    16.         positionDataFilePath = Path.Combine(Application.persistentDataPath, "TEST.csv");
    17.  
    18.         // Write the header line to the position data file
    19.         File.WriteAllText(positionDataFilePath, "TimeStamp,StylusY,StylusZ,StylusX,HeadY,HeadZ,HeadX\n");
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         // Fetch the positions of the VR headset and stylus
    25.         Vector3 currentHeadsetPosition = Head.transform.position;
    26.         Vector3 currentStylusPosition = Stylus.transform.position;
    27.  
    28.         // Format the current positions into a string for the CSV file
    29.         string positionDataLine = string.Format(
    30.             "{0},{1},{2},{3},{4},{5},{6}",
    31.             DateTime.Now,
    32.             currentStylusPosition.x, currentStylusPosition.y, currentStylusPosition.z,
    33.             currentHeadsetPosition.x, currentHeadsetPosition.y, currentHeadsetPosition.z
    34.         );
    35.  
    36.         // Store the formatted string in the list
    37.         positionData.Add(positionDataLine);
    38.     }
    39.  
    40.     public void SavePositionData()
    41.     {
    42.         Debug.Log(positionDataFilePath);
    43.         // Write the list of position data lines to the CSV file after each round
    44.         File.AppendAllLines(positionDataFilePath, positionData);
    45.  
    46.         // Clear the list after saving
    47.         positionData.Clear();
    48.     }
    49. }
    However, when I view the csv file that is created, the proper headings are created, though no actual position data is found:

    upload_2023-7-10_8-46-53.png

    Is there something I am forgetting to reference within my code?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    You're never calling SavePositionData()
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,538
    Well, when do you call SavePositionData ? From a button or something? If not, yes, you don't store any data in your file ^^.
     
  4. VCUMCL

    VCUMCL

    Joined:
    Jun 5, 2017
    Posts:
    24
    So I amended that and called it in the Start function, but I'm still not having any data populate to the csv. I'm thinking maybe I need to call it in a different script?
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,538
    That doesn't make much sense. Start is always called before the first Update method is called. So of course when you save your current list of strings to the file in Start, that list would be empty. When do you actually want to save that list? Maybe when you exit the application? In that case you probably want to save the list in OnApplicationQuit.

    Are you sure you understand your own logic here? ^^
     
    VCUMCL and PraetorBlue like this.
  6. VCUMCL

    VCUMCL

    Joined:
    Jun 5, 2017
    Posts:
    24
    I think I was looking for it to populate in real time rather after the application quits, though this worked. I appreciate the help.
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,538
    Well, in that case you shouldn't even use that list of strings and just write to the file as you go. However you shouldn't use those static methods as they open the file, write to them and then close it again. This is very inefficient. You should open the file and keep the file / StreamWriter open and write to it as you go

    Code (CSharp):
    1. public class WritePositionData : MonoBehaviour
    2. {
    3.     private string positionDataFilePath;
    4.     public GameObject Head;
    5.     public GameObject Stylus;
    6.     private StreamWriter file;
    7.     void Start()
    8.     {
    9.         positionData = new List<string>();
    10.         positionDataFilePath = Path.Combine(Application.persistentDataPath, "TEST.csv");
    11.         file = new StreamWriter(new FileStream(positionDataFilePath, FileMode.Create), Encoding.UTF8);
    12.         // Write the header line to the position data file
    13.         file.WriteLine("TimeStamp,StylusY,StylusZ,StylusX,HeadY,HeadZ,HeadX");
    14.     }
    15.     void Update()
    16.     {
    17.         // Fetch the positions of the VR headset and stylus
    18.         Vector3 currentHeadsetPosition = Head.transform.position;
    19.         Vector3 currentStylusPosition = Stylus.transform.position;
    20.         // Format the current positions into a string for the CSV file
    21.         string positionDataLine = string.Format(
    22.             "{0},{1},{2},{3},{4},{5},{6}",
    23.             DateTime.Now,
    24.             currentStylusPosition.x, currentStylusPosition.y, currentStylusPosition.z,
    25.             currentHeadsetPosition.x, currentHeadsetPosition.y, currentHeadsetPosition.z
    26.         );
    27.         file.WriteLine(positionDataLine);
    28.     }
    29.     void OnApplicationQuit()
    30.     {
    31.         file.Flush();
    32.         file.Close();
    33.     }
    34. }
    This would be more what you want to do.