【问题标题】:Using fscanf in Matlab to read data with some missing info from a text file在 Matlab 中使用 fscanf 从文本文件中读取一些缺失信息的数据
【发布时间】:2018-07-27 07:16:36
【问题描述】:

我有一个文本文件,其内容如下:

0.00316047 0.00189992 0.00186791 0.00170366 0.00145677 0.0020697 0.00316047 0.00287378 0.00226645 1

85.1637 44.9496 59.0738 74.457 82.4159 {} 51.9875 54.7822 85.1637 1

我想在matlab中使用fscanf命令从这个文件中读取数据

可以看出,我想在扫描文本文件并识别它的索引时检测到第二行(即 {})中有一个空 ele。

有什么建议吗?

【问题讨论】:

    标签: matlab scanf info


    【解决方案1】:

    你可以这样做

    fid = fopen('file.txt', 'r');
    s = textscan(fid, '%s', 'Delimiter', ' ', 'MultipleDelimsAsOne', true);
    fclose(fid);
    result = str2double(s{1}).';
    

    这使用空格作为分隔符。结果是一个行向量,其中任何非数字都被转换为NaN。对于您的示例文件,

    result =
      Columns 1 through 6
       0.003160470000000   0.001899920000000   0.001867910000000   0.001703660000000   0.001456770000000   0.002069700000000
      Columns 7 through 12
       0.003160470000000   0.002873780000000   0.002266450000000   1.000000000000000  85.163700000000006  44.949599999999997
      Columns 13 through 18
      59.073799999999999  74.456999999999994  82.415899999999993                 NaN  51.987499999999997  54.782200000000003
      Columns 19 through 20
      85.163700000000006   1.000000000000000
    

    【讨论】:

      猜你喜欢
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多