【问题标题】:Reading large text file using an other file as input format string使用其他文件作为输入格式字符串读取大文本文件
【发布时间】:2012-11-05 02:17:12
【问题描述】:

我有大格式的文本文件 (200 Mb),其中包含有助于轻松读取和保存的数据。格式的周期性约为 72 行,我想要另一个包含 72 行模板格式的文件。有没有办法做到这一点?

理想的方式是

formatstring = fileread(templatefile)

fileToRead = fopen(LargeFile,'r')

while ~feof(fileToRead)

object{i} = textscan(fileToRead,formatstring)

i = i+1
end

模板文件如下所示:

CASE # %16f            DATE: %s

AILERON ANGLE        STAB ANGLE
%4.2f                 %4.2f

ALPHA      BETA     GAMMA

%4.2f      %4.2f      %4.2f

【问题讨论】:

  • 等等,你一个格式文件,想用它来读取你的数据吗?或者您有数据,并且想要创建格式文件?

标签: file matlab formatted-input


【解决方案1】:

如果要读取格式文件并将其转换为有效的格式字符串,请使用:

% read format file
fid = fopen('untitled.txt', 'r');
A = textscan(fid, '%s');
fclose(fid);

% transform into proper format string
A = A{1}(~cellfun('isempty', regexp(A{1}, '%')))
A = [A{:}];

在这种情况下,

>> A
A = 
    %16f%s%4.2f%4.2f%4.2f%4.2f%4.2f

您可以在while-loop 中的textscan 中直接使用它。

【讨论】:

  • 非常感谢。 cellfun 函数总是非常有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多