在Matlab中读取文本文件会导致字符内的未知空格

时间:2015-02-11 23:00:07

标签: matlab vbscript textscan

我正在尝试在Matlab中读取text / 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

1 个答案:

答案 0 :(得分:1)

如果您查看docs,就会看到

  

object.CreateTextFile(filename [,overwrite [,unicode]])

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

(可能是从.OpenTextFile调用中错误复制的)傻瓜.CreateTextFile使用Unicode(证据:BOM,'空格')。

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

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