我有一个要导入到matlab中的 .txt 文件(考虑使用importdata),但是我在告诉matlab格式以及要吸收多少数据时遇到了一些问题。
该文件是从程序“ TurbSim” 生成的。
格式为:
12行标题
1行包含2个数值,间距定界符使用 空的空间。
35行,每行包含35个数值,已完成间距定界符 带有空白。
1行空白行
格式,在标题之后重复,并且我有一个非常大的文件,我无法在脚本中找到一种方法来正确加载该文件,而我无法控制要分割的部分取出。我可能需要该文件,因为文件大小约为860MB。
我的问题的示例txt。 固定 https://drive.google.com/open?id=1FwmrCiz6TaWXYwXYX_v0BwQD-jjbdsE4
答案 0 :(得分:0)
怎么样?
clear;
NUM_HEADERLINES = 12;
DELIM_VALUES = ' ';
fid = fopen('TurbSim.txt');
% skip header
for n = 1:NUM_HEADERLINES, fgets(fid);end
while ~feof(fid)
% read line
[line,nl] = fgets(fid);
% remove newline char
line = line(1:end-length(nl));
% explode using delimiter
values = strsplit(line,DELIM_VALUES);
% in case of leading blanks: skip first empty one
if isempty(values{1}), values = values(2:end);end
% skip blank lines
if isempty(values), continue;end
% convert to double
values = str2double(values);
% now process/save/whatever...
...
fprintf('Read %d values\n',length(values)); % in your example: 2 or 40
% disp(values);
end
fclose(fid);
顺便说一句:您的示例有40行,每行40个值,而不是35行,每行35个。