在matlab中读取具有未格式化元素数的行

在matlab中读取具有未格式化元素数的行,matlab,fopen,textscan,Matlab,Fopen,Textscan,我想阅读每行包含数字的文件。下面是文件格式的示例- boundary-points-x: 0.00 5.00 boundary-points-y: 0.00 0.10 0.20 0.30 4.90 boundary-points-z: 0.00 1.00 2.00 3.00 4.00 5.00 文本后面的数字是我要读取的元素。每一行包含不同数量的元素,每一行中的元素数量可能因文件而异。因此,无法使用textscan以相同的格式进行读取。有办法解决吗?谢谢 用于分析行: fid = fopen(

我想阅读每行包含数字的文件。下面是文件格式的示例-

boundary-points-x: 0.00 5.00
boundary-points-y: 0.00 0.10 0.20 0.30 4.90
boundary-points-z: 0.00 1.00 2.00 3.00 4.00 5.00
文本后面的数字是我要读取的元素。每一行包含不同数量的元素,每一行中的元素数量可能因文件而异。因此,无法使用textscan以相同的格式进行读取。有办法解决吗?谢谢

用于分析行:

fid = fopen('/path/to/file.txt', 'r');
line = fgetl(fid);
while ischar(line)
    res = regexp(line, '\s+(\d*\.?\d*)', 'tokens');
    literals = cellfun(@str2double, res);  % you have this line's literals. You decide what to do with them...
    line = fgetl(fid);
end
fclse(fid);
您可以看到关于正则表达式的解释以及它如何解析行的示例。您还可以在此处使用正则表达式。

用于解析该行:

fid = fopen('/path/to/file.txt', 'r');
line = fgetl(fid);
while ischar(line)
    res = regexp(line, '\s+(\d*\.?\d*)', 'tokens');
    literals = cellfun(@str2double, res);  % you have this line's literals. You decide what to do with them...
    line = fgetl(fid);
end
fclse(fid);
您可以看到关于正则表达式的解释以及它如何解析行的示例。您还可以在那里使用正则表达式