Matlab 固定宽度的文本扫描

标签 matlab textscan

我必须读取这样的文件

10001   3          5.0000      30.0         0.0000      25.6         0.0000      10.0
10002   1         25.0000                   0.0000                   4.6887      58.2
10003   5         45.0000      20.0         0.0000                  14.7608          
10004   5         65.0000                   0.0000                   8.8791          
10005   1         85.0000                   0.0000                   6.3128      00.0

文件格式如下 '%5i%5i%%10.4f%8.1f%10.4f%8.1f%10.4f%8.1f'

我正在使用以下代码

n_xyz_filename = input('\nSelect the file. ', 's');
n_xyz_file = fopen(n_xyz_filename, 'r');
n_xyz = textscan(n_xyz_file, '%5i%5i%10.4f%8.1f%10.4f%8.1f%10.4f%8.1f');
fclose(n_xyz_file);

但是我不断收到以下错误

??? Error using ==> textscan Badly formed format string.

实在是看不懂!

编辑

正如答案所说,正确的代码是:

n_xyz_filename = input('\nSelect the file. ', 's');
n_xyz_file = fopen(n_xyz_filename, 'r');
n_xyz = textscan(n_xyz_file, '%5d%5d%10.4f%8.1f%10.4f%8.1f%10.4f%8.1f');
fclose(n_xyz_file);

用“d”(代表十进制)代替“i”

最佳答案

问题在于格式说明符itextscan 无法识别该格式说明符。如果您想指示一个整数,则应该使用d。因此,正确的语法是:

n_xyz = textscan(n_xyz_file, '%5d%5d%10.4f%8.1f%10.4f%8.1f%10.4f%8.1f');

关于Matlab 固定宽度的文本扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16275869/

相关文章:

matlab - 如何连接 textscan() 加载的不同值?

matlab - 在 Matlab 中拆分时间戳

matlab - imshow 不显示相同的图像

c - matlab和c的cos函数不同

matlab - 乐趣取决于潜艇的顺序和 accumarray 中的值

string - 将复杂的.txt文件读入Matlab

matlab - 与 R 中的类似方法相比,Matlab 中的 textscan 使用过多的 RAM

matlab - 在matlab中安装matConvNet的请求?

matlab - 将 Matlab 数据集数组中的变量设置为单个值

regex - 在Matlab中以最通用的方式从char数组中提取数值数组