简体   繁体   English

在MATLAB中读取格式化的文本

[英]Read formatted text in MATLAB

I have some text files that I want to access them in MATLAB workspace. 我有一些文本文件,我想在MATLAB工作区中访问它们。 MATLAB help says that I can use fscanf , fgetl , and textscan . MATLAB帮助说我可以使用fscanffgetltextscan I chose the last one due to the formatted text. 由于文本的格式,我选择了最后一个。 I wrote the below scripts: 我写了以下脚本:

filename = 'myFile.txt';
fid = fopen(filename);
myData = textscan(fid, '%u64 %{dd/MM/yyyy}D %{hh:mm:ss.SSS}T %f64 %f64 %u64 %f64 %f64 %f64\r\n', 'HeaderLines', 3)
fclose(fid);

but I get the error: 但是我得到了错误:

Error using textscan 使用textscan时出错

Unable to parse the format character vector at position 21 ==> %{HH:mm:ss.SSS}T %f64 %f64 %u64 %f64 %f64 %f64 无法解析位置21 ==>%{HH:mm:ss.SSS} T的格式字符向量%f64%f64%u64%f64%f64%f64

Date formats must be of the form %T or %{...}T. 日期格式必须为%T或%{...} T格式。

The formatted texts are as: 格式文本为:


--------------------------------------------------------------------------------------------------
Row     Var1        Var2           Var3    Var4         Var5         Var6    Var7        Var8 
--------------------------------------------------------------------------------------------------
1       08/04/2018  09:56:52.790   020.00  019.999570   1999690178   055.00  010.020000  000.00000  
2       08/04/2018  09:56:52.821   020.00  019.999602   1999690178   055.00  010.020000  000.00000  
3       08/04/2018  09:56:52.852   020.00  019.999580   1999690178   055.00  010.020000  000.00000  
4       08/04/2018  09:56:52.883   020.00  019.999623   1999690179   055.00  010.020000  000.00000  
5       08/04/2018  09:56:52.915   020.00  019.999548   1999690179   055.00  010.020000  000.00000  
6       08/04/2018  09:56:52.946   020.00  019.999602   1999690179   055.00  010.020000  000.00000  
7       08/04/2018  09:56:52.993   020.00  019.999548   1999690179   055.00  010.020000  000.00000  
8       08/04/2018  09:56:53.024   020.00  019.999602   1999690179   055.00  010.020000  000.00000  
9       08/04/2018  09:56:53.055   020.00  019.999548   1999690179   055.00  010.020000  000.00000  

Use 采用

myData = textscan(fid, '%u64 %{dd/MM/yyyy}D %{hh:mm:ss.SSS}D %f64 %f64 %u64 %f64 %f64 %f64\r\n', 'HeaderLines', 3);

I don't think there is a %T . 我认为这里没有%T %D is datetime (for date and time). %D是日期时间(用于日期和时间)。

I always tend to avoid using fscanf , importdata , textscan and such functions because they can be tricky to deal with and I think their output sometimes is not easy to manipulate. 我总是倾向于避免使用fscanfimportdatatextscan和类似的函数,因为它们可能很难处理,而且我认为它们的输出有时不容易操纵。 On the top of that, your file format looks very similar to the one that Matlab uses for displaying tables data... I think that this is nicenly pointing you to the right direction. 最重要的是,您的文件格式看起来与Matlab用于显示表格数据的格式非常相似...我认为这很好地为您指明了正确的方向。

I recommend you to use readtable , not only because of the aforementioned reasons but also because tables are very versatile in Matlab: 我建议您使用readtable ,这不仅是因为上述原因,还因为表在Matlab中用途非常广泛:

T = readtable('data.txt', ...
      'Format', '%d %{dd/MM/yyyy}D %{HH:mm:ss.SSS}D %f %f %f %f %f %f', ...
      'HeaderLines', 3)

The final output is: 最终输出为:

T =

  9×9 table

    Var1       Var2           Var3        Var4      Var5          Var6       Var7    Var8     Var9
    ____    __________    ____________    ____    _________    __________    ____    _____    ____

    1       08/04/2018    09:56:52.790    20       19.99957    1999690178    55      10.02    0   
    2       08/04/2018    09:56:52.821    20      19.999602    1999690178    55      10.02    0   
    3       08/04/2018    09:56:52.852    20       19.99958    1999690178    55      10.02    0   
    4       08/04/2018    09:56:52.883    20      19.999623    1999690179    55      10.02    0   
    5       08/04/2018    09:56:52.915    20      19.999548    1999690179    55      10.02    0   
    6       08/04/2018    09:56:52.946    20      19.999602    1999690179    55      10.02    0   
    7       08/04/2018    09:56:52.993    20      19.999548    1999690179    55      10.02    0   
    8       08/04/2018    09:56:53.024    20      19.999602    1999690179    55      10.02    0   
    9       08/04/2018    09:56:53.055    20      19.999548    1999690179    55      10.02    0   

PS = the %{...}T format is probably due to a misleading way of handling format error messages from the part of Matlab, only %{...}D is a valid datetime literal format until at least Matlab 2017A. PS = %{...}T格式可能是由于从Matlab部门处理格式错误消息的方式具有误导性,只有%{...}D是有效的日期时间文字格式,至少在Matlab 2017A之前有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM