Reading text file in Matlab results in unknown spaces within characters

女生的网名这么多〃 提交于 2019-12-12 01:09:03

问题


I am trying to read a text/csv file in Matlab.The file looks like:

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

The file is generated using vbscript in Rhinoceros. I am using the standard method given in the help file, but encountering a weird problem.

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

But, my results are not as expected. The tline is storing the strings with a space between each character. Also, there are two unknown characters (ÿþ) at the beginning.

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

The VBScript used to create the textfile looks like this:

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

Can someone help me with this?

Thanks, Amitava


回答1:


If you look at the docs, you'll see

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

Your

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

(probably mis-copied from a .OpenTextFile call) fools .CreateTextFile into using Unicode (evidence: BOM, 'spaces').

So use .CreateTextFile correctly to create (and write to) an ANSI file:

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


来源:https://stackoverflow.com/questions/28466230/reading-text-file-in-matlab-results-in-unknown-spaces-within-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!