如何使用TEXTSCAN在MATLAB中读取文本文件的其余部分? [英] How do I read the rest of a line of a text file in MATLAB with TEXTSCAN?

查看:670
本文介绍了如何使用TEXTSCAN在MATLAB中读取文本文件的其余部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据特定格式读取包含数据的文本文件.我正在使用textscan和一个包含格式的字符串来读取一条代码行中的整个数据集.我已经找到了如何使用fgetl读取整行的内容,但是我想使用尽可能少的代码行.所以我想避免自己的for循环. 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. I've found how to read the whole line with fgetl, but I would like to use as few code lines as possible. So I want to avoid own for loops. textscan seems great for that.

作为示例,我将包含一部分代码,该代码将读取五个字符串,这些字符串代表修改后的数据集,其遗产(旧数据集的名称),修改的日期和时间以及最后的任何注释.

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.

是否可以使用textscan并让我知道我想将一行的其余部分读取为一个字符串/字符数组(包括任何空格)?在我的变量readContentFormat中放置一些内容,而不是最后一个%s.还是有另一种方法不涉及循环遍历文件中的每一行?

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)? I am hoping for something to put in my variable readContentFormat, instead of that last %s. Or is there another method which does not involve looping through each row in the file?

此外,即使我的数据非常有限,我仍然热衷于了解使用不同方法进行计算效率或稳定性方面的利弊.如果您知道自己认为值得分享的内容,请这么做.

推荐答案

一种令我满意的方法(但无论如何请共享其他方法!)是将分隔符设置为除空格以外的其他字符,并修剪掉所有字符strtrim开头或结尾的空格.这似乎很好,但是我不知道计算的要求如何.

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. This seemed to work well, but I have no idea how demanding the computations are.

当前文件夹中的文本文件"testFile.txt"具有以下几行

The text file 'testFile.txt' in the current folder has the following lines


    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

输出:

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!' 

这篇关于如何使用TEXTSCAN在MATLAB中读取文本文件的其余部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆