如何在 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是数据文件的转置。

现在我得到了几个新文件,其中可能有一个以 # 开头的行标题:

# file description
1.0 2.0
1.5 2.2

可以保证 然后标题占用不超过一行,并且始终以 # 开头。

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

textscanf函数似乎能够处理标头,但字段Headerlines的参数是一个固定的数字。

如果您的标头已知以特定字符为前缀,则可以使用 textscan'CommentStyle' NV 对来忽略它们:

具有以下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你可以用fgetl检查文件的第一行,并在必要时使用 frewind (因为fgetl移动文件指针(,如果没有标题,则返回到文件的开头。

例如:

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

这给出了与上面相同的结果。如果标题行的数量不是恒定的,则可以使用带有while循环的ftellfseek跳过标头,但此时您可能会使事情变得比此应用程序实际需要的更复杂。

最新更新