在Matlab读取文本文件导致字符中未知空间(Reading text file in Matlab

2019-11-03 14:32发布

我想读Matlab.The文本/ CSV文件,文件看起来像:

VolumeDisplacement,9783.47
CenterOfBuoyancy,-0.732585,3.16072e-14,-3.09939
WettedSurfaceArea,2709.66
WaterlineLength,102.156
MaximumWaterlineBeam,20.76
WaterPlaneArea,1774.4
CenterOfFloatation,-6.32016,1.00108e-11,0

该文件使用产生vbscript中的Rhinoceros 。 我使用帮助文件中给出的标准方法,但遇到一个奇怪的问题。

filename = 'RhinoResult.txt';
fid = fopen(filename);
line = fgetl(fid);
tline = textscan(line,'%s%d','Delimiter',',');
VolumeDisplacement=tline{2};

但是,并不如预期我的结果。 该tline与每个字符之间的空间存储所述字符串。 此外,还有两个未知字符(ÿþ)开头。

tline{1} = 'ÿþV o l u m e D i s p l a c e m e n t '

VBScript的用于创建文本文件看起来是这样的:

Sub writeResult(arrResults, filePath, fileName)
    Dim objFSO,objFile

    Const ForWriting = 2

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.CreateTextFile(filePath & fileName, _ 
    ForWriting, True)

    objFile.Write "VolumeDisplacement," & arrResults(0)
    objFile.Writeline
    objFile.Write "CenterOfBuoyancy," & arrResults(1)
    objFile.Writeline
    objFile.Write "WettedSurfaceArea," & arrResults(2)
    objFile.Writeline
    objFile.Write "WaterlineLength," & arrResults(3)
    objFile.Writeline
    objFile.Write "MaximumWaterlineBeam," & arrResults(4)
    objFile.Writeline
    objFile.Write "WaterPlaneArea," & arrResults(5)
    objFile.Writeline
    objFile.Write "CenterOfFloatation," & arrResults(6) 
    objFile.Writeline

    objFile.Close

End Sub

有人可以帮我弄这个吗?

谢谢,Amitava

Answer 1:

如果你看一下文档 ,你会看到

object.CreateTextFile(文件名[,覆写[,的unicode]])

你的

Set objFile = objFSO.CreateTextFile(filePath & fileName, _ 
    ForWriting, True)

(可能从.OpenTextFile调用错误复制)傻瓜.CreateTextFile到使用Unicode(证据:BOM,“空间”)。

因此,使用.CreateTextFile正确创建(或写)ANSI文件:

Set objFile = objFSO.CreateTextFile(filePath & fileName, True, False)


文章来源: Reading text file in Matlab results in unknown spaces within characters