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

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

本文介绍了在Matlab中读取文本文件会导致字符内的空格未知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Matlab中读取text/csv文件.该文件如下所示:

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

使用Rhinoceros中的vbscript生成文件.我正在使用帮助文件中提供的标准方法,但是遇到了一个奇怪的问题.

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};

但是,我的结果与预期不符. tline存储字符串,每个字符之间有一个空格.另外,开头有两个未知字符(ÿþ).

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 '

用于创建文本文件的VBScript如下所示:

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

有人可以帮我吗?

谢谢,阿米塔瓦(Amitava)

Thanks,Amitava

推荐答案

如果您查看文档,您将看到

您的

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

(可能是从.OpenTextFile调用中错误复制的),将.CreateTextFile欺骗为使用Unicode(证据:BOM,空格").

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

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

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

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

这篇关于在Matlab中读取文本文件会导致字符内的空格未知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:04