I have a (perhaps) simple question about reading a text file in Unity (C#) containing a matrix of floats, and converting it to a matrix (or multiple arrays) of floats. Before even getting there, I seem unable to succesfully convert the 'string'-typed text to an array using the .Split method.
To summarize, I need the weights of an artificial neural network, which are trained in MATLAB, in my Unity script to use for calculating outputs and performing certain actions.
I will show you carefully what I tried; First of all, the "WeightsIH.txt" file I intend to read looks like this:
-0.493654767886117 1.96685485682002
-0.493654767886117 1.96685485682002
-1.12167737625244 -0.835107290621868
-0.168882176970878 -0.0508678270221517
0.00848314746590752 0.645890421329432
-0.445148017062813 -0.647593931130814
0.0719861441229913 0.00251854104000363
-0.0809087627607675 -0.00253116474618752
0.0677112130287254 0.00229085080614581
0.0754386783608645 0.00239167974789985
0.0809669296303145 0.00253343860819182
-3.54887781611766 -0.884835613897628
-0.459886351851150 -0.848445431434901
-0.0670274486060166 -1.39397672397051
-3.82077442331056 -2.40290337409384
0.0783082838340459 0.00245132734091342
-0.239255503684809 -0.0118048053697862
-0.0798562101865881 -0.00249224979249840
-0.0639184016149756 -0.00224822783089479
-0.778070988555323 -0.872732391008980
0.0297507291420014 -1.74952458318966
0.0963966379119354 0.00416637970011285
0.875794416782941 0.513267873404847
0.0788049456605797 0.00246400296768071
-0.301882135742981 1.29009004735214
-0.427112778202037 -0.602081710823938
-0.0287160010856207 0.876736618413824
0.174484840994587 -0.914135602108887
-1.13771093704277 -1.80803211709460
-0.842897124110693 -0.491320433352398
-0.883862027266628 0.577867664170583
-0.00732465337711950 -0.0608133682721268
0.0808154577205481 0.00252756604265255
-0.623653482256716 0.802021714172196
0.354715179032082 -1.40951283673210
0.107130434979695 0.00718795362864938
-3.25429700675793 1.15179026184665
0.00323132998826740 0.725967105940923
-0.445271160376852 -0.634848835782688
0.0353267566953751 -0.761005808087746
0.343818927585420 0.181552084533698
1.52333372694938 -1.95500299702055
1.28692979700544 2.03963829780562
0.665570336193150 -0.410729910590931
-0.0861536175683719 -0.00286332759826070
0.126619076047933 0.0171533754647685
-0.0822566525595005 -0.00259193055392493
-1.28712374245194 1.12380947288866
-1.29253445353219 -2.05175940341935
0.416493102590467 -0.617909454448863
0.0969179981825367 0.182721981022912
-0.0808788394332979 -0.00252999992128388
0.925818615324062 -1.91710736361040
-0.438361270919922 0.0119396635243124
1.05250770533487 -0.965588245752274
-0.0480257526132498 -0.00154845733604858
-0.0586872685404968 -0.00184430469780941
-0.471992797137374 -0.672492593551730
0.439729645530884 1.55878656878788
1.68156076364744 1.32277838623733
-0.455916176089339 -0.632974493608950
-2.76038741468600 1.87628535124240
0.993963121269136 0.412382925821147
0.0813294944564905 0.00254834378164385
1.05785147371486 -0.713165079912121
0.542621317508334 0.263699015691544
0.0859471661820603 0.00284559667679037
0.0752254002940845 0.00264837949098051
-0.0821494531270125 -0.00258646752960803
-0.135286303232541 -0.0230503985166856
-1.04824146276167 0.379479302836832
-1.00411135626130 0.643815076295448
-1.06427192746705 -1.71485567599474
0.0306923117644997 -0.326399702175058
-0.269230352435044 1.15492815472443
-1.09071040094221 0.974587786723127
-0.0811137162377503 -0.00253932111442298
0.843300471645878 -0.443547135621555
2.62543922875650 -1.43287453338882
-0.0879222032042109 -0.00305697486042674
1.08943207983567 -0.751402936369758
-0.0807147111768526 -0.00252376120454932
0.0920673615786699 0.00345537914425264
-3.32572250550751 2.23334579177979
0.567347532732561 -0.849919751538726
-0.981291377531284 -1.65375737600341
0.717607651399507 -0.501535458162733
Now my C# Unity code looks like this:
using UnityEngine;
using System.Collections;
using System;
using System.IO;
public class WeightsMDF : MonoBehaviour
{
private string text;
void Start ()
{
FileInfo theSourceFile = new FileInfo
("C:/Users/Ajdin/Downloads/UnitySpace/minorProject/Nerd/Nerds/Assets/Scenes/WeightsIH.txt");
StreamReader reader = theSourceFile.OpenText();
do
{
text = reader.ReadLine();
string[] floats = text.Split(" "[0]);
//Console.WriteLine(text);
print (text);
//print(floats[0]);
} while (text != null);
}
}
Now this code gives me the entire textfile in the console, but with a nullreferenceexeption at last: NullReferenceException: Object reference not set to an instance of an object WeightsMDF.Start () (at Assets/Scripts/Nerds/WeightsMDF.cs:19).
This exception refers to line 19:
string[] floats = text.Split(" "[0]);
Now if I comment this line, I get the same output except the last console output line says
Null
And If I change line 19 to:
string[] floats = text.Split(" ");//[0]);
I get the following compiling error:
Assets/Scripts/Nerds/WeightsMDF.cs(19,36): error CS1502: The best overloaded method match for `string.Split(params char[])' has some invalid arguments
Assets/Scripts/Nerds/WeightsMDF.cs(19,36): error CS1503: Argument `#1' cannot convert `string' expression to type `char[]'
Does anyone know what I'm doing wrongly? Thanks in advance!