为什么Octave textscan跳过CSV文件中的最后一列?

问题描述 投票:0回答:1

我有以下MATLAB代码:

fid = fopen(filename, 'r');
lineFormat = [repmat('%s',1,1), ...
        repmat('%f',1,1), ...
        repmat('%q',1,1), ...
        repmat('%f',1,1), ...
        repmat('%q',1,1)];
C = textscan(fid, lineFormat, 'Delimiter',',', 'HeaderLines',15, 'CommentStyle','%');

当我的输入数据是CSV文件时:

% <15 header lines omitted>
102,19700101,,0.485,
111,19700101,,0.48,

在MATLAB中,我的脚本产生了以下结果(预期):

>> C
C =

  1×5 cell array

    {2×1 cell}    {2×1 double}    {2×1 cell}    {2×1 double}    {2×1 cell}

>> C{2}

ans =

    19700101
    19700101

>> C{5}

ans =

  2×1 cell array

    {0×0 char}
    {0×0 char}

虽然在八度音阶中,我得到了不同的结果:

C = 
{
  [1,1] = 
  {
    [1,1] = 102
    [2,1] = 19700101
  }
  [1,2] =

     19700101
          NaN

  [1,3] = 
  {
    [1,1] = 
    [2,1] = 0.48
  }
  [1,4] =

     0.48500
         NaN

  [1,5] = 
  {
    [1,1] = 111
    [2,1] = 
  }
}

为什么八度切换输入,有效地忽略了CSV中每行的最后一个空值? textscan是否应该在Octave和MATLAB之间产生相同的输出?有没有一种方法可以在Octave中使用textscan,使其行为与MATLAB相同?

matlab octave
1个回答
0
投票

默认情况下,八度使用C0修剪空白。我不确定为什么要修剪最后一个值(因为CSV的结尾逗号应表示最后一列的值是空字符串textscan)。我在实验时发现此功能与MATLAB不同。

要解决此问题,请将''参数添加到whitespace调用中。

替换为:

textscan

使用此:

C = textscan(fid, lineFormat, 'Delimiter',',', 'HeaderLines',15, 'CommentStyle','%');

参考:C = textscan(fid, lineFormat, 'Delimiter',',', 'HeaderLines',15, 'CommentStyle','%', 'whitespace',''); (请参阅页面底部的https://octave.sourceforge.io/octave/function/textscan.html选项]

© www.soinside.com 2019 - 2024. All rights reserved.