Matlab无法读取这种类型的混合格式文本?

发布于 2024-11-14 17:20:25 字数 3368 浏览 1 评论 0原文

我需要使用 Matlab 读取格式奇怪的文本文件。第一列是年份,第二列是月份,第三列是日期

1948 9 9   23.0600    5.1100    0.0371   25.6667   17.8500
1948 910    2.6800    5.0720    0.0398   23.1889   16.7889
1948 911    0.1500    5.0350    0.0371   27.3222   15.7556
1948 912    0.0000    4.9970    0.0318   30.1833   14.0278
1948 913    0.1000    4.9590    0.0292   31.0444   16.0889
1948 914    0.0000    4.9210    0.0255   28.8167   19.4833
1948 915    0.0800    4.8820    0.0255   31.3000   18.2389
1948 916    0.9500    4.8440    0.0255   31.8278   15.5722
1948 917    6.7100    4.8050    0.0292   31.1611   18.2667
1948 918    1.7100    4.7650    0.0292   29.8278   20.2389
1948 919    0.5500    4.7260    0.0292   30.6389   20.0222
1948 920    0.0000    4.6860    0.0255   32.1167   19.3500
1948 921    5.5700    4.6460    0.0255   32.9111   19.0333
1948 922    3.0300    4.6060    0.0255   32.3833   18.4056
1948 923    0.2700    4.5660    0.0255   32.2278   17.2389
1948 924    0.0000    4.5260    0.0255   32.6889   17.2500
1948 925    0.0000    4.4850    0.0255   31.5056   16.0833
1948 926    0.0000    4.4440    0.0223   27.6333   12.4778
1948 927    0.0000    4.4040    0.0191   26.4000    9.7000
1948 928    0.0000    4.3630    0.0223   26.4556    8.4333
1948 929    0.0000    4.3220    0.0223   28.6778    9.8444
1948 930    0.0000    4.2810    0.0223   30.8222    9.3389
194810 1    0.0000    4.2400    0.0255   31.9000    9.9222
194810 2    0.0000    4.1990    0.0292   31.7000   10.1222
194810 3    0.0000    4.1570    0.0292   31.0056   10.8778
194810 4    0.3400    4.1160    0.0255   31.2278    9.7722
194810 5    0.0000    4.0750    0.0255   30.4222   10.1944
194810 6    0.0000    4.0330    0.0255   32.3389   12.8778
194810 7    0.0000    3.9920    0.0223   27.8222   16.2833
194810 8    0.0000    3.9510    0.0223   29.3611   13.8444
194810 9    0.0000    3.9100    0.0223   30.7722   14.2833
19481010    0.0000    3.8680    0.0223   29.8833   17.7722
19481011    0.0000    3.8270    0.0223   29.3444   17.6722
19481012    0.0000    3.7860    0.0223   30.2444   12.3389
19481013    0.1200    3.7450    0.0223   31.6056   12.5056
19481014    0.0400    3.7040    0.0223   33.6556   14.0667
19481015    2.4400    3.6630    0.0223   32.7222   16.8278
19481016   23.7700    3.6230    0.0260   32.3333   17.8333
19481017   41.4600    3.5820    1.3760   19.4667    7.4500
19481018    0.0000    3.5410    0.2757   17.9889    4.1556
19481019    0.1600    3.5010    0.0954   21.5944    4.4667
19481020    0.8900    3.4610    0.0530   23.5389    6.6056
19481021    0.4100    3.4210    0.0451   22.0556   11.4222
19481022    0.1000    3.3810    0.0424   24.8889   13.4333
19481023    0.0000    3.3410    0.0398   24.6778   10.6889
19481024    0.0000    3.3020    0.0371   24.3222   10.3444
19481025    0.0300    3.2630    0.0371   25.0611    8.4167
19481026    0.0400    3.2240    0.0371   24.4944    6.0444
19481027    0.0600    3.1850    0.0371   25.4722    5.8722
19481028    0.0700    3.1470    0.0345   24.6056   10.8500
19481029    0.0100    3.1080    0.0345   26.1556   14.1667
19481030    0.0900    3.0700    0.0345   27.2944   18.0556
19481031    0.0200    3.0330    0.0318   27.9167   18.1278

这是我迄今为止没有成功的代码 fid = fopen('data.txt','r'); 数据 = fscanf(fid, '%4d%2d%2d %f %f %f %f %f', [8 inf]); fclose(fid);

任何建议将不胜感激

催化剂

I need to read a text file with strange format using Matlab. The 1st column is Year, 2nd column is Month and 3rd column is Day

1948 9 9   23.0600    5.1100    0.0371   25.6667   17.8500
1948 910    2.6800    5.0720    0.0398   23.1889   16.7889
1948 911    0.1500    5.0350    0.0371   27.3222   15.7556
1948 912    0.0000    4.9970    0.0318   30.1833   14.0278
1948 913    0.1000    4.9590    0.0292   31.0444   16.0889
1948 914    0.0000    4.9210    0.0255   28.8167   19.4833
1948 915    0.0800    4.8820    0.0255   31.3000   18.2389
1948 916    0.9500    4.8440    0.0255   31.8278   15.5722
1948 917    6.7100    4.8050    0.0292   31.1611   18.2667
1948 918    1.7100    4.7650    0.0292   29.8278   20.2389
1948 919    0.5500    4.7260    0.0292   30.6389   20.0222
1948 920    0.0000    4.6860    0.0255   32.1167   19.3500
1948 921    5.5700    4.6460    0.0255   32.9111   19.0333
1948 922    3.0300    4.6060    0.0255   32.3833   18.4056
1948 923    0.2700    4.5660    0.0255   32.2278   17.2389
1948 924    0.0000    4.5260    0.0255   32.6889   17.2500
1948 925    0.0000    4.4850    0.0255   31.5056   16.0833
1948 926    0.0000    4.4440    0.0223   27.6333   12.4778
1948 927    0.0000    4.4040    0.0191   26.4000    9.7000
1948 928    0.0000    4.3630    0.0223   26.4556    8.4333
1948 929    0.0000    4.3220    0.0223   28.6778    9.8444
1948 930    0.0000    4.2810    0.0223   30.8222    9.3389
194810 1    0.0000    4.2400    0.0255   31.9000    9.9222
194810 2    0.0000    4.1990    0.0292   31.7000   10.1222
194810 3    0.0000    4.1570    0.0292   31.0056   10.8778
194810 4    0.3400    4.1160    0.0255   31.2278    9.7722
194810 5    0.0000    4.0750    0.0255   30.4222   10.1944
194810 6    0.0000    4.0330    0.0255   32.3389   12.8778
194810 7    0.0000    3.9920    0.0223   27.8222   16.2833
194810 8    0.0000    3.9510    0.0223   29.3611   13.8444
194810 9    0.0000    3.9100    0.0223   30.7722   14.2833
19481010    0.0000    3.8680    0.0223   29.8833   17.7722
19481011    0.0000    3.8270    0.0223   29.3444   17.6722
19481012    0.0000    3.7860    0.0223   30.2444   12.3389
19481013    0.1200    3.7450    0.0223   31.6056   12.5056
19481014    0.0400    3.7040    0.0223   33.6556   14.0667
19481015    2.4400    3.6630    0.0223   32.7222   16.8278
19481016   23.7700    3.6230    0.0260   32.3333   17.8333
19481017   41.4600    3.5820    1.3760   19.4667    7.4500
19481018    0.0000    3.5410    0.2757   17.9889    4.1556
19481019    0.1600    3.5010    0.0954   21.5944    4.4667
19481020    0.8900    3.4610    0.0530   23.5389    6.6056
19481021    0.4100    3.4210    0.0451   22.0556   11.4222
19481022    0.1000    3.3810    0.0424   24.8889   13.4333
19481023    0.0000    3.3410    0.0398   24.6778   10.6889
19481024    0.0000    3.3020    0.0371   24.3222   10.3444
19481025    0.0300    3.2630    0.0371   25.0611    8.4167
19481026    0.0400    3.2240    0.0371   24.4944    6.0444
19481027    0.0600    3.1850    0.0371   25.4722    5.8722
19481028    0.0700    3.1470    0.0345   24.6056   10.8500
19481029    0.0100    3.1080    0.0345   26.1556   14.1667
19481030    0.0900    3.0700    0.0345   27.2944   18.0556
19481031    0.0200    3.0330    0.0318   27.9167   18.1278

This is the code I have so far without any success
fid = fopen('data.txt','r');
data = fscanf(fid, '%4d%2d%2d %f %f %f %f %f', [8 inf]);
fclose(fid);

Any suggestion would be gratefully acknowledged

thecatalyst

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温暖的光 2024-11-21 17:20:25

空格的分布意味着您必须更加努力才能正确读取这些数据。这似乎对我有用:

data = [];
fid = fopen('test.txt', 'r');
while ~feof(fid)
    data = [ data ;
             sscanf(fgets(fid, 4), '%d') ...
             sscanf(fgets(fid, 2), '%d') ...
             sscanf(fgets(fid, 2), '%d') ...
             sscanf(fgetl(fid), '%f %f %f %f %f')' ];
end
fclose(fid);

The distribution of whitespace means you'll have to work a little bit harder to read this data correctly. This seems to work for me:

data = [];
fid = fopen('test.txt', 'r');
while ~feof(fid)
    data = [ data ;
             sscanf(fgets(fid, 4), '%d') ...
             sscanf(fgets(fid, 2), '%d') ...
             sscanf(fgets(fid, 2), '%d') ...
             sscanf(fgetl(fid), '%f %f %f %f %f')' ];
end
fclose(fid);
驱逐舰岛风号 2024-11-21 17:20:25

问题可能是第二列和第三列没有正确填充,是的,月份和日期列相互连接,所以您需要首先修复它。

The problem is likely that the second and third columns are not properly padded, the yeah, month and day colums go into eachother, so you need to fix that first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文