如何在 MATLAB 中读取可能 headers 的文件?

How to read files with possible headers in MATLAB?

最初我的文件如下:

1.4 2.0
4.2 2.1
5.1 1.2

列号固定,行号因文件而异。以下代码可以读取这些文件:

fid = fopen("my_file.txt","r");
M = fscanf(fid,"%f",[2,inf]);

这里M是数据文件的转置

现在我得到几个新文件,可能有一行 header 以 #:

开头
# file description
1.0 2.0
1.5 2.2

保证然后header不超过一行,并且始终以#开头。

我知道我可以逐行读取文件来处理 headers。我想知道是否有任何方法可以对我的原始代码进行尽可能少的更改,以便新代码可以读取两种格式的文件。

textscanf 函数似乎可以处理 headers,但字段 Headerlines 的参数是一个固定数字。

如果您的 header 已知以特定字符为前缀,那么您可以使用 textscan's 'CommentStyle' NV-pair 忽略它们:

与以下test.txt:

# A header line
1 2
3 4
5 6

我们可以使用:

fID = fopen("test.txt", "r");
M = textscan(fID, "%f", "CommentStyle", "#");
M = reshape(M{:}, 2, []).';
fclose(fID)

这给了我们:

>> M

M =

     1     2
     3     4
     5     6

或者,如果你想坚持使用 fscanf you can check the first line of the file with fgetl and use frewind,如果有必要(因为 fgetl 移动文件指针),如果没有 [=43] 则返回到文件开头=].

例如:

fID = fopen("test.txt", "r");

% Test for header
tline = fgetl(fID);  % Moves file pointer to next line
commentchar = "#";
if strcmp(tline(1), commentchar)
    % Header present, read from line 2
    M = fscanf(fID, "%f", [2, inf]).';
else
    % Header present, rewind to beginning of file & read as before
    frewind(fID);
    M = fscanf(fID, "%f", [2, inf]).';
end
fclose(fID);

这给出了与上面相同的结果。如果 header 行的数量不是常量,您可以使用 ftell and fseekwhile 循环来跳过 header 行,但此时您可能会使事情变得比这个应用程序真正需要的更复杂。