我有一个关于在Unity中读取包含浮点数矩阵的文本文件,并将其转换为浮点数矩阵(或多个数组)的(可能)简单问题。在尝试之前,我似乎无法成功地使用.Split方法将’text’类型的文本转换为数组。
简而言之,我需要在Unity脚本中使用从MATLAB中训练得到的人工神经网络的权重,用于计算输出和执行某些操作。
我将仔细展示我尝试过的方法;首先,我打算读取的“WeightsIH.txt”文件看起来像这样:
-0.493654767886117 1.96685485682002-0.493654767886117 1.96685485682002-1.12167737625244 -0.835107290621868-0.168882176970878 -0.05086782702215170.00848314746590752 0.645890421329432-0.445148017062813 -0.6475939311308140.0719861441229913 0.00251854104000363-0.0809087627607675 -0.002531164746187520.0677112130287254 0.002290850806145810.0754386783608645 0.002391679747899850.0809669296303145 0.00253343860819182-3.54887781611766 -0.884835613897628-0.459886351851150 -0.848445431434901-0.0670274486060166 -1.39397672397051-3.82077442331056 -2.402903374093840.0783082838340459 0.00245132734091342-0.239255503684809 -0.0118048053697862-0.0798562101865881 -0.00249224979249840-0.0639184016149756 -0.00224822783089479-0.778070988555323 -0.8727323910089800.0297507291420014 -1.749524583189660.0963966379119354 0.004166379700112850.875794416782941 0.5132678734048470.0788049456605797 0.00246400296768071-0.301882135742981 1.29009004735214-0.427112778202037 -0.602081710823938-0.0287160010856207 0.8767366184138240.174484840994587 -0.914135602108887-1.13771093704277 -1.80803211709460-0.842897124110693 -0.491320433352398-0.883862027266628 0.577867664170583-0.00732465337711950 -0.06081336827212680.0808154577205481 0.00252756604265255-0.623653482256716 0.8020217141721960.354715179032082 -1.409512836732100.107130434979695 0.00718795362864938-3.25429700675793 1.151790261846650.00323132998826740 0.725967105940923-0.445271160376852 -0.6348488357826880.0353267566953751 -0.7610058080877460.343818927585420 0.1815520845336981.52333372694938 -1.955002997020551.28692979700544 2.039638297805620.665570336193150 -0.410729910590931-0.0861536175683719 -0.002863327598260700.126619076047933 0.0171533754647685-0.0822566525595005 -0.00259193055392493-1.28712374245194 1.12380947288866-1.29253445353219 -2.051759403419350.416493102590467 -0.6179094544488630.0969179981825367 0.182721981022912-0.0808788394332979 -0.002529999921283880.925818615324062 -1.91710736361040-0.438361270919922 0.01193966352431241.05250770533487 -0.965588245752274-0.0480257526132498 -0.00154845733604858-0.0586872685404968 -0.00184430469780941-0.471992797137374 -0.6724925935517300.439729645530884 1.558786568787881.68156076364744 1.32277838623733-0.455916176089339 -0.632974493608950-2.76038741468600 1.876285351242400.993963121269136 0.4123829258211470.0813294944564905 0.002548343781643851.05785147371486 -0.7131650799121210.542621317508334 0.2636990156915440.0859471661820603 0.002845596676790370.0752254002940845 0.00264837949098051-0.0821494531270125 -0.00258646752960803-0.135286303232541 -0.0230503985166856-1.04824146276167 0.379479302836832-1.00411135626130 0.643815076295448-1.06427192746705 -1.714855675994740.0306923117644997 -0.326399702175058-0.269230352435044 1.15492815472443-1.09071040094221 0.974587786723127-0.0811137162377503 -0.002539321114422980.843300471645878 -0.4435471356215552.62543922875650 -1.43287453338882-0.0879222032042109 -0.003056974860426741.08943207983567 -0.751402936369758-0.0807147111768526 -0.002523761204549320.0920673615786699 0.00345537914425264-3.32572250550751 2.233345791779790.567347532732561 -0.849919751538726-0.981291377531284 -1.653757376003410.717607651399507 -0.501535458162733
现在我的C# Unity代码如下所示:
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); }}
现在这段代码会在控制台中输出整个文本文件,但最后会出现一个空引用异常:NullReferenceException: Object reference not set to an instance of an objectWeightsMDF.Start () (at Assets/Scripts/Nerds/WeightsMDF.cs:19)。
这个异常指的是第19行:
string[] floats = text.Split(" "[0]);
如果我注释掉这一行,我会得到相同的输出,只是最后的控制台输出行显示
Null
如果我将第19行改为:
string[] floats = text.Split(" ");//[0]);
我会得到以下编译错误:
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[]'
有谁知道我做错了什么?提前感谢!
回答:
在使用split之前,请先检查text。 ReadLine
在文件末尾返回 null
,所以你需要在分割之前检查它。
text = reader.ReadLine();if (text == null) break;string[] floats = text.Split(" "[0]);
此外,你可以使用 Peek
。它返回下一个符号而不使用它,如果没有剩余符号则返回 -1
。
using (StreamReader sr = new StreamReader(path)) { while (sr.Peek() >= 0) { text = sr.ReadLine(); var splits = text.Split (new string[] {" "}, StringSplitOptions.RemoveEmptyEntries); } }
要按空白字符分割字符串,你可以使用
string[] splits = text.Split(null);string[] splits = text.Split(new char[0]);