简体   繁体   English

如何使用TEXTSCAN在MATLAB中读取文本文件的其余部分?

[英]How do I read the rest of a line of a text file in MATLAB with TEXTSCAN?

I am trying to read a text file with data according to a specific format. 我正在尝试根据特定格式读取包含数据的文本文件。 I am using and textscan together with a string containing the format to read the whole data set in one code line. 我正在使用textscan和一个包含格式的字符串,以在一个代码行中读取整个数据集。 I've found how to read the whole line with fgetl , but I would like to use as few code lines as possible. 我发现了如何使用fgetl读取整行,但是我想使用尽可能少的代码行。 So I want to avoid own for loops. 所以我想避免自己的for循环。 textscan seems great for that. textscan似乎很棒。

As an example I'll include a part of my code which reads five strings representing a modified dataset, its heritage (name of old dataset), the date and time of the modification and lastly any comment. 作为示例,我将包含代码的一部分,该代码将读取代表修改后的数据集的五个字符串,其遗产(旧数据集的名称),修改的日期和时间以及最后的任何注释。

fileID = fopen(filePath,'r+');
readContentFormat = '%s = %s | %s %s | %s';
content = textscan(fileID, readContentFormat, 'CollectOutput,1);

This works for the time being if the comment doesn't have any delimiters (like a white space) in it. 如果注释中没有任何定界符(例如空格),则此操作暂时有效。 However, I would like to be able to write comments at the end of the line. 但是,我希望能够在该行的末尾写评论。

Is there a way to use textscan and let it know that I want to read the rest of a line as one string/character array (including any white spaces)? 有没有一种方法可以使用textscan并让它知道我想将一行的其余部分读取为一个字符串/字符数组(包括任何空格)? I am hoping for something to put in my variable readContentFormat , instead of that last %s . 我希望在变量readContentFormat放入一些readContentFormat ,而不是最后的%s Or is there another method which does not involve looping through each row in the file? 还是有另一种方法不涉及循环遍历文件中的每一行?

Also, even though my data is very limited I am keen to know any pros or cons with different methods regarding computational efficiency or stability. 另外,即使我的数据非常有限,我还是希望了解有关计算效率或稳定性的不同方法的利弊。 If you know something you think is worth sharing, please do so. 如果您知道值得共享的内容,请这样做。

One way that is satisfactory to me (but please share any other methods anyway!) is to set the delimiters to characters other than white space, and trim away any leading or trailing white spaces with strtrim . 一种令我满意的方法(但无论如何也请共享其他方法!)是将分隔符设置为除空格以外的其他字符,并使用strtrim修剪所有前导或尾随空格。 This seemed to work well, but I have no idea how demanding the computations are. 这似乎工作得很好,但是我不知道计算的要求如何。


Example: 例:

The text file 'testFile.txt' in the current folder has the following lines 当前文件夹中的文本文件“ testFile.txt”具有以下几行

File        |Heritage       |Date and time         |Comment
      file1.mat |  oldFile1.mat |  2018-03-01 14:26:00 |  -
      file2.mat |  oldFile2.mat |  2018-03-01 13:26:00 |  -
      file3.mat |  oldFile3.mat |  2018-03-01 12:26:00 |  Time for lunch!

The following code will read the data and put it into a cell array without leading or trailing white spaces, with few lines of code. 以下代码将读取数据并将其放入单元格数组中,而无需使用前导或尾随空格,只需几行代码。 Neat! 整齐!

function contentArray = myfun()
   fileID = fopen(testFile.txt,'r');
   content = textscan(fileID, '%s%s%s%s','Delimiter', {'|'},'CollectOutput', 1);
   contentArray =  strtrim(content{1}(2:4,:));
end

The output: 输出:

tmpArr =

  3×4 cell array

    'file1.mat'    'oldFile1.mat'    '2018-03-01 14:26:00'    '-'            
    'file2.mat'    'oldFile2.mat'    '2018-03-01 13:26:00'    '-'
    'file3.mat'    'oldFile3.mat'    '2018-03-01 12:26:00'    'Time for lunch!' 

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

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