How to export in a text file float numbers generated in a list
Hello everyone,
I'm totally new to coding. I am trying to develop a psychological test based on Reaction Times. In a script I created a list that during the session gets filled with 7 float numbers. The list is...
public List reactionTimes = new List();
After the session I would like to export the 7 reaction times in a text file on my Mac. So I created another Script, attached to an empty object in Unity, named SaveData. I was trying to experiment on this following some online tutorials but I can't figure out how it is intended to be done. Even though there is no error reported on the Scripts, nothing seems to happen. This is the code, adding using System.IO.
public class SaveData : MonoBehaviour {
ReactionTime subjectresults;
// Use this for initialization
void Start () {
subjectresults = GetComponent <ReactionTime>();
}
// Update is called once per frame
void Update () {
}
private void SaveToFile()
{
StreamWriter writer = new StreamWriter (@"path");
foreach (float TR in subjectresults.reactionTimes)
{
float output = TR;
writer.WriteLine (output);
writer.Close ();
}
}
}
What can I do to fix this? Thank you very much
Answer by Hellium · Feb 12, 2019 at 04:11 PM
You are closing the writer
before writing all the lines....
Use the following method instead:
private void SaveToFile()
{
// Convert the floats to strings
string[] lines = new string[subjectresults.reactionTimes.Length];
for( int i = 0 ; i < subjectresults.reactionTimes.Length ; ++i )
lines[i] = subjectresults.reactionTimes[i].ToString();
// Write the lines into the file
File.WriteAllLines(Application.persistentDataPath + "/reactionTimes.txt", lines);
}
Hello @Hellium thank you for answering,
I tried using your method but I still can't find a way to save this .txt on my desktop (for example). Actually I can't find the .txt anywhere on my $$anonymous$$ac (unfortunately I don't have a PC at the moment). Do you have any idea about what I'm missing?
Thanks
On $$anonymous$$ac, Application.persistentDataPath
points to /var/mobile/Containers/Data/Application/<guid>/Documents
If you want to save the file on the Desktop, you will have to change the path.
I am really not sure of the following, but you can give it a try:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
File.WriteAllLines(path + "/reactionTimes.txt", lines);
Hello again @Hellium , I still can't figure out why this file I'm trying to save doesn't exist apparently anywhere on my mac. This is my actual code... The only differences from yours are: reactionTimes.Count ins$$anonymous$$d of "Lenght", because reactionTimes is a List, and I've added System.Environment, because with only Environment it was returning an error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class SaveData : $$anonymous$$onoBehaviour {
ReactionTime subjectresults;
// Use this for initialization
void Start () {
subjectresults = GetComponent <ReactionTime>();
}
// Update is called once per frame
void Update () {
}
private void SaveToFile()
{
// Convert the floats to strings
string[] lines = new string[subjectresults.reactionTimes.Count];
for (int i = 0; i < subjectresults.reactionTimes.Count; ++i)
lines [i] = subjectresults.reactionTimes [i].ToString ();
// Write the lines into the file
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
File.WriteAllLines (path + "/reactionTimes.rtf", lines);
}
}
How and when do ou call SaveToFile
? Try to print the path in the SaveToFile
function.
Hi @Hellium ,
Thank you for your help. I realized I wasn't calling for SaveToFile. So I called it in the update function and it worked!
Have a good day!
Don't call it too often in the Update
function, only when you need it! Input/output operations are heavy.
If the answer solved your problem, don't forget to click on the Accept button below it
Could it be reasonable to call it in the Updated function only when the list reaches 7 elements, which are the 7 reaction times that I need?
I suggest you using a "flag" to know when the file has been saved:
bool fileSaved = false;
void Update ()
{
if( subjectresults.reactionTimes.Count == 7 && !fileSaved )
{
SaveToFile();
fileSaved = true;
}
}
Ok, but the condition !&fileSaved might not work with my last intent: I don't want to take too much of your time, but is it possible to start from the script as it is now to save - in the same text file - other Reaction Times calculated in other Unity Scenes, also specifying from which one I'm calculating them? I know I don't have enough competences to do this, but if this script is a good starting point I'll try to check some tutorials to finish this project!
Thank you