【问题标题】:Remove specific value from .txt file while using (readtable) MATLAB使用(可读)MATLAB 时从 .txt 文件中删除特定值
【发布时间】:2018-03-27 06:19:14
【问题描述】:

我在 MATLAB 中的 TSPLIB .txt 文件上使用 readtable 函数时遇到了问题。我一直在寻找这个问题的解决方案,但我的问题似乎与其他问题不同。

问题是我尝试导入的每个 .txt 文件都以“EOF”作为最后 3 个字符。当我从 .txt 文件中手动删除此值时,readtable 通过从文件中的值创建一个表来完全满足我的要求。我正在使用以下代码读取文件,但是当文件中仍然存在“EOF”时,此代码将不起作用:

data = readtable('d493.txt', 'HeaderLines', 6, 'Format', '%f %f %f')

如何使用代码从每个文件中删除“EOF”?

【问题讨论】:

    标签: matlab


    【解决方案1】:

    一个简单的选项是利用readtable'CommentStyle' 选项将以'EOF' 开头的行视为cmets 并忽略它们。

    例如,使用以下asdf.txt

    header1, header2
    123, 456
    789, 123
    EOF
    

    我们可以根据需要使用readtable 加载我们的文件:

    >> test = readtable('asdf.txt', 'CommentStyle', 'EOF')
    
    test =
    
      2×2 table
    
        header1    header2
        _______    _______
    
        123        456    
        789        123    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 2014-08-18
      • 1970-01-01
      • 2019-02-10
      • 2018-06-16
      相关资源
      最近更新 更多