在 Matlab 中使用 textscan 处理格式不正确的数据

Using textscan in Matlab to handle data not properly formatted data

我正在使用 textscan 导入数据。我可以成功导入格式正确的数据。我无法让它正确处理格式不正确的数据。下面是数据的格式。

JeB2021Da 12-13 and stuff, 1, 1, 0, 1, 0, 1, 1, 1, 3, 1, 99, 0, 0, 0, 
JoB2021Ha 12-13 and stuff, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 99, 2, 1, 0, 
JoP2021Co 12-13 and stuff, not enough samples
MaA2021Be 12-13 and stuff, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 99, 1, 0, 0, 
MaA2021Ma 12-13 and stuff, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 99, 1, 0, 0,

我将如何处理 not enough samples 数据?因为目前数据结构没有对齐。正在生成的数据结构是 17 x 116 x 14。我想按数据中的原样导入字符串。所以 not enough samples 将被导入。下面是我正在使用的代码。

fid = fopen('./file.txt','r');
fmt = ['%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d'];
d = textscan(fid, fmt, 'CollectOutput', 1,'Delimiter',',','headerLines', 1, 'EmptyValue', 0);

我正在尝试使用 EmptyValue 标志来处理它,但它不起作用。非常感谢任何帮助。

我不确定您所说的 我想按数据中的原样导入字符串,或者更确切地说,您希望该字符串位于何处存储。

但如果只是读取整个数据,您可以使用 'TreatAsEmpty' 参数:

d = textscan(fid, fmt, 'CollectOutput', 1,'Delimiter',',','headerLines', 1,'TreatAsEmpty','not enough samples');

然后您可以通过在导入的数据数组中查找仅由零组成的行来进一步修改输入。