Import and modify " .txt" files
1 回表示 (過去 30 日間)
古いコメントを表示
Hi
I have some ".txt" files with this structure :
useless
useless
useless
...
data
data
...
useless
useless
useless
data
...
And I would like to have a .txt file like this :
data
data
data
...
I just know that each first line of data section begins with '1' but the length of the headerline (useless) is not fixed ! So I would like to iterate until lines begins with '1' and then keep the data.
How would you do that the easiest way ?
Thanks a lot
5 件のコメント
回答 (2 件)
Thorsten
2015 年 11 月 26 日
Copy those lines that have four numbers:
fid = fopen('file.txt', 'r');
fid2 = fopen('file2.txt', 'w');
line = fgets(fid);
while line ~= -1
[~, count] = sscanf(line, '%f');
if count == 4
fprintf(fid2, '%s', line);
end
line = fgets(fid);
end
fclose(fid)
fclose(fid2)
6 件のコメント
johnmay
2015 年 11 月 26 日
2 件のコメント
Walter Roberson
2015 年 11 月 27 日
Before the loop:
n = 2; %whatever is appropriate
fmt = repmat('%f ', 1, n);
fmt(end:end+1) = '\n'; %newline, unrelated to the variable 'n'
In the loop:
[data, count] = sscanf(line, '%f');
if count >= n
fprintf(fid2, fmt, data(1:n));
end
参考
カテゴリ
Help Center および File Exchange で Model Import についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!