Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello !

Here my code :

C#
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using UnityEngine;
using System;


public class Recup_donnees_6bis : MonoBehaviour {

	private class Data_struct {

		public String marker_name ; 
		public Vector3 [] positions ; 


	}
		private static Data_struct [] nv_data ;

		static GameObject [] body ; 
		string[][] datas; 

		int cpt = 0 ; 


	// When the script instance is being loaded

	void Awake ()

	{
		Application.targetFrameRate = 25; 
	}


	// Use this for initialization

	void Start() 

	{
		body = new GameObject[55]; 

		body [0] = GameObject.Find ("RightUpLeg"); 
		body [1] = GameObject.Find ("LeftUpLed");
		body [5] = GameObject.Find ("Neck");
		body [6] = GameObject.Find ("RightArm");
		body [7] = GameObject.Find ("LeftArm");
		body [8] = GameObject.Find ("Throat");
		body [9] = GameObject.Find ("Spine1");
		body [11] = GameObject.Find ("Spine");
		body [12] = GameObject.Find ("Hips");
		body [13] = GameObject.Find ("Scalp");
		body [14] = GameObject.Find ("HeadTop_End");
		body [15] = GameObject.Find ("R_Temple");
		body [16] = GameObject.Find ("L_Temple");
		body [17] = GameObject.Find ("RightForeArm");
		body [21] = GameObject.Find ("RightHand");
		body [23] = GameObject.Find ("RightHandIndex1");
		body [24] = GameObject.Find ("RightHandThumb2");
		body [25] = GameObject.Find ("RightHandPinky2");
		body [26] = GameObject.Find ("LeftForeArm");
		body [30] = GameObject.Find ("LeftHand");
		body [32] = GameObject.Find ("LeftHandIndex1");
		body [33] = GameObject.Find ("LeftHandThumb2");
		body [34] = GameObject.Find ("LeftHandPinky2");
		body [35] = GameObject.Find ("RightLed");
		body [38] = GameObject.Find ("RightFoot");
		body [41] = GameObject.Find ("RightFootToeBase_End");
		body [42] = GameObject.Find ("RightToeBase");
		body [45] = GameObject.Find ("LeftLed");
		body [48] = GameObject.Find ("LeftFoot");
		body [51] = GameObject.Find ("LeftFootToeBase_End");
		body [53] = GameObject.Find ("LeftToeBase");

		StreamReader reader = new StreamReader ("Suj01_PI_DP_C00_1.txt"); 
		
		using (reader) { 
			
			string line = " "; 
			int lineNumber = 10; 

				for (int i = 0; i < lineNumber; i++) 
				{ 
					line = reader.ReadLine(); 
					if (line == null) return; 
				}

			string line_10;
			line_10 = line; 
			string[] names = line_10.Split (new String[] {",",",,,"},StringSplitOptions.RemoveEmptyEntries); 

			nv_data = new Data_struct[names.Length] ;

				for (int i =0 ; i< names.Length; ++i )
				{
					nv_data[i] = new Data_struct () ;
					nv_data[i].marker_name = names[i] ; 
				}

			line = reader.ReadLine();
			string line_11;
			 line_11 = line;
			string[] axes = line_11.Split(new String[] {"Field #",","},StringSplitOptions.RemoveEmptyEntries);

			datas = new string[4000][]; 

			int counter = 0;
			while (line != null) { 
				counter++;
				line = reader.ReadLine();

				if (line == "ANALOG") 
				break ;

				if ((counter %3) != 1) 
					continue;

				string lines_datas; 
				lines_datas = line;
				datas[cpt] = lines_datas.Split(new string[] {","},StringSplitOptions.RemoveEmptyEntries);

				line = reader.ReadLine(); 

				cpt ++ ;

			}

				for (int i = 0 ; i < cpt ; ++i ) 
				{
					for (int j = 1 ; j < names.Length+1 ; j++ ) 
				{
					nv_data[j-1].positions = new Vector3[cpt]; 

				nv_data[j-1].positions[i].x = float.Parse(datas[i][j*3-2]); 
				nv_data[j-1].positions[i].y = float.Parse(datas[i][j*3-1]);
				nv_data[j-1].positions[i].z = float.Parse(datas[i][j*3]);

				}

			}

		}
		
	 }

	// Update is called once per frame
	void Update () {

		for (int i = 0; i < cpt; ++i) {
			for (int k = 0; k < body.Length; ++k)
			{
				if(body == null)
					Debug.Log("body is null!");

				if(body[k] == null)
					Debug.Log("body["+k+"] is null!");
				
				if(body[k].transform == null)
					Debug.Log("body["+k+"].transform is null!");		
				
				if(nv_data == null)
					Debug.Log("nv_data is null!");
				
				if(nv_data[k] == null)
					Debug.Log("nv_data["+k+"] is null!");
	
				body[k].transform.position = nv_data[k].positions[i];
				Debug.Log(body[1].transform.position);
			}

		}

	}

}

My problems appear in "void update ()":

My first problem has been about : body[k].transform.position = nv_data[k].positions[i];
It's for this line that the message in title of this question will appear.

I have tried to find the problem, so I have used different "Debug.Log". Errors which appear are :

"body [1] is null !" so my problem is about body[k]
"nv_data is null !" so my problem is about nv_data (it appears when I stop the animation)
"Object reference not set to an instance of an Object" for the line : if(body[k].transform == null)

Could you help me please ?

Thank you

P-s : I'm not english and I'm a beginner in C#
Posted
Updated 10-Mar-15 0:32am
v4
Comments
StM0n 26-Feb-15 5:32am    
I'm not quite sure, but what is the meaning of <<lineNumber>>?
StM0n 26-Feb-15 6:01am    
You're not moving forward to your expected line... lineNumber is just an integer. You have to step forward to your line, e.g. repeat reader.ReadLine() eleven times (bad example, I know Smile | :) )
Coralie B 26-Feb-15 7:34am    
Is there another solution ? To tell that I have to repeat this line eleven times
Sinisa Hajnal 26-Feb-15 8:09am    
I don't think s_mon meant literally repeat. Use the loop until ReadLine returns nothing...
Coralie B 26-Feb-15 8:13am    
How can I use "loop" ?

Ok, let me give you a 'possible' solution:

1-If there are a lot of, and not well-know, of possible values to save, don't use a matrix. I'd rather use a DataTable. It allows to add a lot of data and get ordered values later.

I write some textfile corresponding to your structure, in fact, assuming this is the correct format.

Before to use your own text file be sure it match the correct schema.

Suj01_PI_DP_C00_1
Date:,2014/11/06
Time:,14:55:12
Type:,test
Description:,""
Notes:,""
 
TRAJECTORIES
100.000000,Hz
,Suj01:RIAS,,,Suj01:LIAS,,,Suj01:LIPS
Field #,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X
1,-242.807816,1106.551270,1097.119385,14.437944,1075.778687,1097.119385,14.437944,1075.778687
100.000000,Hz
,Suj01:RIAS,,,Suj01:RIPS,,,Suj01:LIPS
Field #,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X
3,-242.807816,1106.551270,1097.119385,14.437944,1075.778687,1097.119385,14.437944,1075.778687
100.000000,Hz
,Suj01:RIAS,,,Suj01:DSTR,,,Suj01:C7
Field #,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X
1,-242.807816,1106.551270,1097.119385,14.437944,1075.778687,1097.119385,14.437944,1075.778687
100.000000,Hz
,Suj01:LIAS,,,Suj01:LIPS,,,Suj01:C7
Field #,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X
5,-242.807816,1106.551270,1097.119385,14.437944,1075.778687,1097.119385,14.437944,1075.778687


And this is the code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test1
{
	class Program
	{
		static int Main(string[] args)
		{
			// define one table and it schema
			//
			System.Data.DataTable tdata = new System.Data.DataTable();
			tdata.Columns.Add("Name", typeof(string));
			tdata.Columns.Add("X", typeof(double));
			tdata.Columns.Add("Y", typeof(double));
			tdata.Columns.Add("Z", typeof(double));

			System.IO.StreamReader reader = new System.IO.StreamReader(@"..\..\TextFile1.txt");

			string line = "", captions = "", values = "";
			bool wrong_header = false;
						
			using ( reader )
			{
				// document header, Date, Time, etc... 
				// Do you need to save this info?
				//
				for (int x = 0; x < 8; x++)
				{
					if ((line = reader.ReadLine()) == null)
					{
						wrong_header = true;
						break;
					}
				}

				// header error,
				//
				if (wrong_header)
				{
					// error msg.....
					//
					return -1;
				}

				// i supouse this 4 lines always repeats
				//
				while (true)
				{
					// 100.0000,Hz
					if ((line = reader.ReadLine()) == null) break;

					// caption
					if ((captions = reader.ReadLine()) == null) break;

					// X,Y,Z stuff
					if ((line = reader.ReadLine()) == null) break;

					// values
					if ((values = reader.ReadLine()) == null) break;

					// split captions and values
					// as far I can see ",,," is the separator value
					string[] scap = captions.Split(new string [] {",,,"}
													, StringSplitOptions.RemoveEmptyEntries);
					string[] sval = values.Split(new char [] {','});

					// save every sample
					int pos_value = 0;
					foreach(string caption in scap)
					{
						System.Data.DataRow row = tdata.NewRow();

						// remove leading ','
						row["Name"] = caption.Substring(1, caption.Length - 1);
						// if you want to remove Sju01:
						// row["Name"] = caption.Substring(7, caption.Length - 1);

						// it must be 3 values (x,y,z) for every captin
						row["X"] = Convert.ToDouble(sval[pos_value++]);
						row["Y"] = Convert.ToDouble(sval[pos_value++]);
						row["Z"] = Convert.ToDouble(sval[pos_value++]);

						// add the row to the table
						tdata.Rows.Add(row);
					}
				}
			}

			// now you have a table in this form
			//
			// 'RIAS',X,Y,Z
			// 'RIAS',X,Y,Z
			// 'LIAS',X,Y,Z
			// 'LIPS',X,Y,Z
			// 'C7',X,Y,Z
			// 'DSTR',X,Y,Z

			// how to get all data ordered by name
			//
			Console.WriteLine("Ordered by name");
			System.Data.DataRow[] v_order_by_name = tdata.Select("", "Name");
			foreach(System.Data.DataRow rsort in v_order_by_name)
			{
				string msg = string.Format("{0}: {1},{2},{3}", rsort[0],rsort[1],rsort[2],rsort[3]);
				Console.WriteLine(msg);
			}
			Console.WriteLine();
			Console.WriteLine();

			// how to get data filtered by name
			//
			Console.WriteLine("Filtered by name RIAS");
			System.Data.DataRow[] v_filtered_by_name = tdata.Select("Name = 'Suj01:RIAS'", "");
			foreach (System.Data.DataRow rfilter in v_filtered_by_name)
			{
				string msg = string.Format("{0}: {1},{2},{3}", rfilter[0], rfilter[1], rfilter[2], rfilter[3]);
				Console.WriteLine(msg);
			}


			Console.WriteLine("Press any key.");
			Console.ReadKey();

			return 0;
		}
	}
}


And this is the result:

CSS
Ordered by name
Suj01:LIAS: 5,-242.807816,1106.55127
Suj01:RIAS: 1,-242.807816,1106.55127
Suj01:RIAS: 3,-242.807816,1106.55127
Suj01:RIAS: 1,-242.807816,1106.55127
uj01:C7: 1097.119385,14.437944,1075.778687
uj01:C7: 1097.119385,14.437944,1075.778687
uj01:DSTR: 1097.119385,14.437944,1075.778687
uj01:LIAS: 1097.119385,14.437944,1075.778687
uj01:LIPS: 1097.119385,14.437944,1075.778687
uj01:LIPS: 1097.119385,14.437944,1075.778687
uj01:LIPS: 1097.119385,14.437944,1075.778687
uj01:RIPS: 1097.119385,14.437944,1075.778687


Filtered by name RIAS
Suj01:RIAS: 1,-242.807816,1106.55127
Suj01:RIAS: 3,-242.807816,1106.55127
Suj01:RIAS: 1,-242.807816,1106.55127
Press any key.


I hope it helps you.

Salut
 
Share this answer
 
Comments
Coralie B 27-Feb-15 7:50am    
Your are extraordinary ! Thank you a much ! It's very great that you help me like this.

I'm going to see this solution in more details after my comment.

But, first I see these problems :
- When I copy your program, these sentences appear in red : (everything between '')
System.Data.'DataTable' tdata = new System.Data.'DataTable'();
tdata.'Columns.Add'("Name", typeof(string));
tdata.'Columns.Add'("X", typeof(double));
tdata.'Columns.Add'("Y", typeof(double));
tdata.'Columns.Add'("Z", typeof(double));
System.Data.'DataRow' row = tdata.'NewRow'();
tdata.'Rows.Add'(row);
And the other 'DataRow'

- And I want to take datas in commands line, but after the line number "1," "2," ... How can I write that for the program ?
- Furthermore, I want to take only 1 line / 4 (because the frequency of the file was 100 Fps, but me in Unity I used 25 Fps). I know how I indicate 25 Fps in the program, but how can I write that detail in the program ?

Thank you a much again !!!
Joan Magnet 27-Feb-15 8:44am    
You must add some references to your project. Go to Sulution Explorer, click right button over References and select Add Reference and choose:

System.Data

To save some data of the first 8 lines, simply use some variables and modify the loop:

// document header, Date, Time, etc...

// Do you need to save this info?
//
for (int x = 0; x < 8; x++)
{
if ((line = reader.ReadLine()) == null)
{
if (x == DesiredLine)
{
// save your data
}
wrong_header = true;
break;
}
}

To save only 1 line in 4:

- Add a variable before while(true)

int fpsLap = 0;

// i supouse this 4 lines always repeats
//
while (true)
{

and check it after reading the values line:

// values
if ((values = reader.ReadLine()) == null) break;

// only pass 1st time or when rest of integer division = 0
fpsLap++;
if (fpsLap != 1 && ((fpsLap-1) % 4) != 0) continue;

I've not verified it, but it should work.

Please, rate my solution if you like it.

Thnaks.
Coralie B 27-Feb-15 8:59am    
// Add a variable here
int fpsLap = 0;

// The loop
while (true)
{

// values
if ((values = reader.ReadLine()) == null) break;

// only pass 1st time or when rest of integer division = 0
fpsLap++;
if (fpsLap != 1 && ((fpsLap-1) % 4) != 0) continue;

I integrate these lines in the program ? To save 1 line / 4, it's from the beginning of datas lines (positions values). Is it what you had written ? And I need to don't extract "1," ; "2," ; "3,"; ... value concerning the number of datas line.

About the first lines, I don't need to save these informations. I need from the line where there are names "Suj01:RIAS" ... to the end. But before it's not important and I don't need it.

Thank you a much again, and I had to add "system.data", thank you a much for that too.
Joan Magnet 27-Feb-15 9:05am    
If you don't need 1,2 and 3 then change it for:

if (fpsLap % 4 != 0) continue;

Only lines 4, 8, 12, and so on will be saved.

The old code save: 1, 5, 9, etc...
Coralie B 27-Feb-15 9:20am    
So the old code if it saves : 1,5,9 etc ... it's good for this detail ! If it concerns only datas lines.

I hope that you see what I want to mean
Here is my adaptation of your class...this one reads each line, if you need every fourth, simply call line = reader.ReadLine(), increase the counter instantiated outside of the loop and before doing any parsing check if counter % your_constant == 0.

C#
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;

public class Recup_donnees_3 : MonoBehaviour {
  // When the script instance is being loaded
  void Awake ()
  {
    Application.targetFrameRate = 25; // J'indique que la fréquence est de 25 Hz
  }

  class FileReader
  {
    static void Main()
    {
    // ------------- This first part allows me to read in the console and to load in a matrix 1 line / 4 from the 12th line of the text file to the end ------------- //
    // Create an instance of StreamReader to read from a file
    StreamReader reader = new StreamReader ("Suj01_PI_DP_C00_1.txt");

    using (reader) { // Automatic Closing of the Stream after working with it , otherwise we would have to write "reader.Close" at the end to close the stream, beneath the last command's line of this part "class FileReader"

      int lineNumber = 10;
    string line;
      // skip first 10 lines
      for (i = 0; i < lineNumber; i++) {

        line = reader.ReadLine();
        if (line == null) return; // this ensures that the file has at least 12 lines
    }

      // at this point our line contains row #10 (Suj01:RIAS,,,Suj01:LIAS,,,Suj01:LIPS,,,Suj01:RIPS,,,Suj01:DSTR,,,Suj01:C7,,,Suj01:RACR,,,)
      // do whatever you need to do
    string[] Matrice_Suj01_PI_DP_C00_1; // To create a matrix which contains 1 line / 4 from text file
    Matrice_Suj01_PI_DP_C00_1 = line;
    string[] values_position_X_Y_Z = Matrice_Suj01_PI_DP_C00_1.Split(',',3); // Pb : we can't use "Split" when it's a string []

    line = reader.ReadLine(); // here it is line #11 (Field #,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X)
    // parse it and use it however you need
    line_11 = line;
    string[] axes = line_11.Split(new char[] {'Filed #',','}); // Every axe is separating by ',' and the first is writing behind 'Filed #,'

    line = reader.ReadLine(); // here the loop starts getting data

    int counter = 0;
    while (line != null) {
        counter++;
        if (counter % 4) != 1 continue;
    // here you're beginning with the lines you need ot enter into the matrix
    // (1,-242.807816,1106.551270,1097.119385,14.437944,1075.778687,)
    // Do whatever you need to do with the line data

    line = reader.ReadLine(); // after you're done, this needs to be last line in the while so you can continue the loop
      }
    }
  }
}
 
Share this answer
 
v2
Comments
Coralie B 2-Mar-15 2:24am    
First, Thank you a much for your program ! It's very great to help me .

How can I do to indicate that I want to read only 1 line / 4 from the beginning of datas lines ?

Then, there are some mistakes in your program.

For example, we can't use "Split" when the int is a [](array, matrix):
string[] axes = line_11.Split(new char[] {'Filed #',','});
Here "Split" appears in red.

Futhermore, you used the int "line" several times. So, in the program it appears wrong, "line" is red.

I need to don't save the number of the lines.
line = reader.ReadLine(); // here the loop starts getting data
while (line != null) {
// here you're beginning with the lines you need ot enter into the matrix
// (1,-242.807816,1106.551270,1097.119385,14.437944,1075.778687,)
1, -> It's the number of line, and for every line that I save, I have to don't get this number and the ","

Your program allows me to organize every data in group ? For each group of 3 datas, it corresponds to a name "Suj01:RIAS", ... and every data corresponds to a position value ? The first to X, the second to Y and the third to Z ? That in a loop ? Until we have saved every 1 line / 4
Sinisa Hajnal 2-Mar-15 3:40am    
As I said, if you need every fourth line, add the counter variable, increase it at the start of the loop and check that counter % 4 = 1 (thus, it will parse lines 1, 5, 9 etc)

line is a string, not int, I don't know what you're trying to tell me...my program doesn't do anything with the data, I've just orgranized your class so that you get what you asked for, that is, some lines to parse the names and values, other lines to parse into the matrix, but the parsing itself I leave to you, it is your program, you know how to take your data and fill them wherever you need.
Coralie B 2-Mar-15 4:02am    
I don't know how I can indicate the counter.
Like this ?
int counter %4==0;
line = reader.Readline();
counter %4==1;
while ...

If line is a string, can I use line=reader.Readline ? I think it's correct only if line is an int, no ?
And I can't use string[] axes = line_11.Split(new char[] {'Filed #',','}); because "Split" appears in red.
Sinisa Hajnal 2-Mar-15 6:12am    
line is a string. ReadLine returns the string (whole line of text).
as for the counter:
int counter = 0;
while() {
counter++;
line = reader.ReadLine();
if (counter %4) <> 1 then continue;
// if it is exactly 1 (lines 1, 5, 9 etc) continue working
}
Coralie B 2-Mar-15 7:39am    
Thank you very much !

I have ever this problem... Line appears in red

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900