0

我有一个文本文件,其内容如下:

0.00316047 0.00189992 0.00186791 0.00170366 0.00145677 0.0020697 0.00316047 0.00287378 0.00226645 1

85.1637 44.9496 59.0738 74.457 82.4159 {} 51.9875 54.7822 85.1637 1

我想在 matlab 中使用 fscanf 命令从这个文件中读取数据

可以看出,在扫描文本文件并识别它的索引时,我想在第二行(即 {})中检测到一个空 ele。

有什么建议吗?

4

1 回答 1

1

你可以做类似的事情

fid = fopen('file.txt', 'r');
s = textscan(fid, '%s', 'Delimiter', ' ', 'MultipleDelimsAsOne', true);
fclose(fid);
result = str2double(s{1}).';

这使用空间作为分隔符。结果是一个行向量,其中任何非数字都被转换为NaN. 对于您的示例文件,

result =
  Columns 1 through 6
   0.003160470000000   0.001899920000000   0.001867910000000   0.001703660000000   0.001456770000000   0.002069700000000
  Columns 7 through 12
   0.003160470000000   0.002873780000000   0.002266450000000   1.000000000000000  85.163700000000006  44.949599999999997
  Columns 13 through 18
  59.073799999999999  74.456999999999994  82.415899999999993                 NaN  51.987499999999997  54.782200000000003
  Columns 19 through 20
  85.163700000000006   1.000000000000000
于 2018-07-26T23:16:36.713 回答